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) ){ 40b026e05eSdrh sqlite3VdbeMemExpandBlob(p); 419310ef23Sdrh p->flags &= ~MEM_Str; 421f0feef8Sdrh p->flags |= MEM_Blob; 434f26d6c4Sdrh return p->z; 444f26d6c4Sdrh }else{ 454f26d6c4Sdrh return sqlite3_value_text(pVal); 464f26d6c4Sdrh } 474f26d6c4Sdrh } 484f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 4913073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 504f26d6c4Sdrh } 514f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 5213073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 534f26d6c4Sdrh } 544f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 556a6124e2Sdrh return sqlite3VdbeRealValue((Mem*)pVal); 564f26d6c4Sdrh } 574f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 586a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 594f26d6c4Sdrh } 60efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 616a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 624f26d6c4Sdrh } 634f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 6400fd957bSdanielk1977 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 654f26d6c4Sdrh } 666c62608fSdrh #ifndef SQLITE_OMIT_UTF16 674f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 68dc8453fdSdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 694f26d6c4Sdrh } 70d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 71d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 72d8123366Sdanielk1977 } 73d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 74d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 75d8123366Sdanielk1977 } 766c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 774f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 78f4479501Sdrh return pVal->type; 794f26d6c4Sdrh } 8029d72108Sdrh /* sqlite3_value_numeric_type() defined in vdbe.c */ 814f26d6c4Sdrh 824f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 834f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 844f26d6c4Sdrh ** the function result. 854f26d6c4Sdrh */ 864f26d6c4Sdrh void sqlite3_result_blob( 874f26d6c4Sdrh sqlite3_context *pCtx, 884f26d6c4Sdrh const void *z, 894f26d6c4Sdrh int n, 90d8123366Sdanielk1977 void (*xDel)(void *) 914f26d6c4Sdrh ){ 925708d2deSdrh assert( n>=0 ); 93d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); 944f26d6c4Sdrh } 954f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 964f26d6c4Sdrh sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 974f26d6c4Sdrh } 984f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 994f26d6c4Sdrh pCtx->isError = 1; 100d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 1014f26d6c4Sdrh } 102a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16 1034f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 1044f26d6c4Sdrh pCtx->isError = 1; 105d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 1064f26d6c4Sdrh } 107a1686c9aSdanielk1977 #endif 108f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 1094f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 1104f26d6c4Sdrh } 1114f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 1124f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 1134f26d6c4Sdrh } 1144f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 115f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 1164f26d6c4Sdrh } 1174f26d6c4Sdrh void sqlite3_result_text( 1184f26d6c4Sdrh sqlite3_context *pCtx, 1194f26d6c4Sdrh const char *z, 1204f26d6c4Sdrh int n, 121d8123366Sdanielk1977 void (*xDel)(void *) 1224f26d6c4Sdrh ){ 123d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); 1244f26d6c4Sdrh } 1256c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1264f26d6c4Sdrh void sqlite3_result_text16( 1274f26d6c4Sdrh sqlite3_context *pCtx, 1284f26d6c4Sdrh const void *z, 1294f26d6c4Sdrh int n, 130d8123366Sdanielk1977 void (*xDel)(void *) 1314f26d6c4Sdrh ){ 132d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); 133d8123366Sdanielk1977 } 134d8123366Sdanielk1977 void sqlite3_result_text16be( 135d8123366Sdanielk1977 sqlite3_context *pCtx, 136d8123366Sdanielk1977 const void *z, 137d8123366Sdanielk1977 int n, 138d8123366Sdanielk1977 void (*xDel)(void *) 139d8123366Sdanielk1977 ){ 140d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); 141d8123366Sdanielk1977 } 142d8123366Sdanielk1977 void sqlite3_result_text16le( 143d8123366Sdanielk1977 sqlite3_context *pCtx, 144d8123366Sdanielk1977 const void *z, 145d8123366Sdanielk1977 int n, 146d8123366Sdanielk1977 void (*xDel)(void *) 147d8123366Sdanielk1977 ){ 148d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); 1494f26d6c4Sdrh } 1506c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1514f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 1524f26d6c4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 1534f26d6c4Sdrh } 154b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 155b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); 156b026e05eSdrh } 1574f26d6c4Sdrh 158a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */ 159a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 160a0206bc8Sdrh sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1); 161a0206bc8Sdrh } 162a0206bc8Sdrh 1634f26d6c4Sdrh 1644f26d6c4Sdrh /* 1654f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 1664f26d6c4Sdrh ** statement is completely executed or an error occurs. 167b900aaf3Sdrh ** 168b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step() 169b900aaf3Sdrh ** API. The only thing omitted is the automatic recompile if a 170b900aaf3Sdrh ** schema change has occurred. That detail is handled by the 171b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure. 1724f26d6c4Sdrh */ 173b900aaf3Sdrh static int sqlite3Step(Vdbe *p){ 1749bb575fdSdrh sqlite3 *db; 1754f26d6c4Sdrh int rc; 1764f26d6c4Sdrh 177efaaf579Sdanielk1977 /* Assert that malloc() has not failed */ 1789e12800dSdanielk1977 assert( !sqlite3MallocFailed() ); 179261919ccSdanielk1977 1801bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ 1814f26d6c4Sdrh return SQLITE_MISUSE; 1824f26d6c4Sdrh } 18391b48aa1Sdrh if( p->aborted ){ 18491b48aa1Sdrh return SQLITE_ABORT; 18591b48aa1Sdrh } 186a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 187a21c6b6fSdanielk1977 if( p->rc==SQLITE_OK ){ 188a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 189a21c6b6fSdanielk1977 } 190b900aaf3Sdrh rc = SQLITE_ERROR; 191b900aaf3Sdrh goto end_of_step; 192a21c6b6fSdanielk1977 } 1934f26d6c4Sdrh db = p->db; 1944f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1954f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1964f26d6c4Sdrh return SQLITE_MISUSE; 1974f26d6c4Sdrh } 1981d850a72Sdanielk1977 if( p->pc<0 ){ 19915ca1df1Sdrh /* If there are no other statements currently running, then 20015ca1df1Sdrh ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 20115ca1df1Sdrh ** from interrupting a statement that has not yet started. 20215ca1df1Sdrh */ 20315ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 20415ca1df1Sdrh db->u1.isInterrupted = 0; 20515ca1df1Sdrh } 20615ca1df1Sdrh 20719e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 208c16a03b5Sdrh /* Invoke the trace callback if there is one 209c16a03b5Sdrh */ 21019e2d37fSdrh if( db->xTrace && !db->init.busy ){ 211c16a03b5Sdrh assert( p->nOp>0 ); 212c16a03b5Sdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 213c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3!=0 ); 214c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 215c16a03b5Sdrh sqlite3SafetyOff(db); 216c16a03b5Sdrh db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); 217c16a03b5Sdrh if( sqlite3SafetyOn(db) ){ 218c16a03b5Sdrh p->rc = SQLITE_MISUSE; 219c16a03b5Sdrh return SQLITE_MISUSE; 220c16a03b5Sdrh } 221c16a03b5Sdrh } 22219e2d37fSdrh if( db->xProfile && !db->init.busy ){ 22319e2d37fSdrh double rNow; 22466560adaSdrh sqlite3OsCurrentTime(&rNow); 22519e2d37fSdrh p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; 22619e2d37fSdrh } 22719e2d37fSdrh #endif 228c16a03b5Sdrh 229c16a03b5Sdrh /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned 230c16a03b5Sdrh ** on in debugging mode. 231c16a03b5Sdrh */ 232c16a03b5Sdrh #ifdef SQLITE_DEBUG 233c16a03b5Sdrh if( (db->flags & SQLITE_SqlTrace)!=0 ){ 234c16a03b5Sdrh sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); 235c16a03b5Sdrh } 236c16a03b5Sdrh #endif /* SQLITE_DEBUG */ 237c16a03b5Sdrh 2381d850a72Sdanielk1977 db->activeVdbeCnt++; 2391d850a72Sdanielk1977 p->pc = 0; 2401d850a72Sdanielk1977 } 241b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 2424f26d6c4Sdrh if( p->explain ){ 2434f26d6c4Sdrh rc = sqlite3VdbeList(p); 244b7f9164eSdrh }else 245b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 246b7f9164eSdrh { 2474f26d6c4Sdrh rc = sqlite3VdbeExec(p); 2484f26d6c4Sdrh } 2494f26d6c4Sdrh 2504f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 2514f26d6c4Sdrh rc = SQLITE_MISUSE; 2524f26d6c4Sdrh } 2534f26d6c4Sdrh 25419e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 25519e2d37fSdrh /* Invoke the profile callback if there is one 25619e2d37fSdrh */ 25719e2d37fSdrh if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ 25819e2d37fSdrh double rNow; 25919e2d37fSdrh u64 elapseTime; 26019e2d37fSdrh 26166560adaSdrh sqlite3OsCurrentTime(&rNow); 26219e2d37fSdrh elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; 26319e2d37fSdrh assert( p->nOp>0 ); 26419e2d37fSdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 26519e2d37fSdrh assert( p->aOp[p->nOp-1].p3!=0 ); 26619e2d37fSdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 26719e2d37fSdrh db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); 26819e2d37fSdrh } 26919e2d37fSdrh #endif 27019e2d37fSdrh 27197a227c9Sdanielk1977 sqlite3Error(p->db, rc, 0); 27276e8d1a2Sdanielk1977 p->rc = sqlite3ApiExit(p->db, p->rc); 273b900aaf3Sdrh end_of_step: 2744ac285a1Sdrh assert( (rc&0xff)==rc ); 275b900aaf3Sdrh if( p->zSql && (rc&0xff)<SQLITE_ROW ){ 276b900aaf3Sdrh /* This behavior occurs if sqlite3_prepare_v2() was used to build 277b900aaf3Sdrh ** the prepared statement. Return error codes directly */ 278b900aaf3Sdrh return p->rc; 279b900aaf3Sdrh }else{ 280b900aaf3Sdrh /* This is for legacy sqlite3_prepare() builds and when the code 281b900aaf3Sdrh ** is SQLITE_ROW or SQLITE_DONE */ 28276e8d1a2Sdanielk1977 return rc; 2834f26d6c4Sdrh } 284b900aaf3Sdrh } 285b900aaf3Sdrh 286b900aaf3Sdrh /* 287b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step(). Call 288b900aaf3Sdrh ** sqlite3Step() to do most of the work. If a schema error occurs, 289b900aaf3Sdrh ** call sqlite3Reprepare() and try again. 290b900aaf3Sdrh */ 291b900aaf3Sdrh #ifdef SQLITE_OMIT_PARSER 292b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 293b900aaf3Sdrh return sqlite3Step((Vdbe*)pStmt); 294b900aaf3Sdrh } 295b900aaf3Sdrh #else 296b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 297b900aaf3Sdrh int cnt = 0; 298b900aaf3Sdrh int rc; 299b900aaf3Sdrh Vdbe *v = (Vdbe*)pStmt; 300b900aaf3Sdrh while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 301b900aaf3Sdrh && cnt++ < 5 302b900aaf3Sdrh && sqlite3Reprepare(v) ){ 303b900aaf3Sdrh sqlite3_reset(pStmt); 304b900aaf3Sdrh v->expired = 0; 305b900aaf3Sdrh } 306b900aaf3Sdrh return rc; 307b900aaf3Sdrh } 308b900aaf3Sdrh #endif 3094f26d6c4Sdrh 3104f26d6c4Sdrh /* 311eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 312eb2e176aSdrh ** pointer to it. 313eb2e176aSdrh */ 314eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 315eb2e176aSdrh assert( p && p->pFunc ); 316eb2e176aSdrh return p->pFunc->pUserData; 317eb2e176aSdrh } 318eb2e176aSdrh 319eb2e176aSdrh /* 320b7481e70Sdrh ** The following is the implementation of an SQL function that always 321b7481e70Sdrh ** fails with an error message stating that the function is used in the 322b7481e70Sdrh ** wrong context. The sqlite3_overload_function() API might construct 323b7481e70Sdrh ** SQL function that use this routine so that the functions will exist 324b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction 325b7481e70Sdrh ** method of virtual tables. 326b7481e70Sdrh */ 327b7481e70Sdrh void sqlite3InvalidFunction( 328b7481e70Sdrh sqlite3_context *context, /* The function calling context */ 329b7481e70Sdrh int argc, /* Number of arguments to the function */ 330b7481e70Sdrh sqlite3_value **argv /* Value of each argument */ 331b7481e70Sdrh ){ 332b7481e70Sdrh const char *zName = context->pFunc->zName; 333b7481e70Sdrh char *zErr; 334b7481e70Sdrh zErr = sqlite3MPrintf( 335b7481e70Sdrh "unable to use function %s in the requested context", zName); 336b7481e70Sdrh sqlite3_result_error(context, zErr, -1); 337b7481e70Sdrh sqliteFree(zErr); 338b7481e70Sdrh } 339b7481e70Sdrh 340b7481e70Sdrh /* 341eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 342eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 343eb2e176aSdrh ** same context that was returned on prior calls. 344eb2e176aSdrh */ 345eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 346abfcea25Sdrh Mem *pMem = p->pMem; 347825c662eSdrh assert( p && p->pFunc && p->pFunc->xStep ); 348a10a34b8Sdrh if( (pMem->flags & MEM_Agg)==0 ){ 349a10a34b8Sdrh if( nByte==0 ){ 350a10a34b8Sdrh assert( pMem->flags==MEM_Null ); 351a10a34b8Sdrh pMem->z = 0; 352a10a34b8Sdrh }else{ 353abfcea25Sdrh pMem->flags = MEM_Agg; 3545360ad34Sdrh pMem->xDel = sqlite3FreeX; 3553c024d69Sdrh pMem->u.pDef = p->pFunc; 356eb2e176aSdrh if( nByte<=NBFS ){ 357abfcea25Sdrh pMem->z = pMem->zShort; 358abfcea25Sdrh memset(pMem->z, 0, nByte); 359eb2e176aSdrh }else{ 360abfcea25Sdrh pMem->z = sqliteMalloc( nByte ); 361eb2e176aSdrh } 362eb2e176aSdrh } 363a10a34b8Sdrh } 364abfcea25Sdrh return (void*)pMem->z; 365eb2e176aSdrh } 366eb2e176aSdrh 367eb2e176aSdrh /* 368682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 369682f68b0Sdanielk1977 ** the user-function defined by pCtx. 370682f68b0Sdanielk1977 */ 371682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 372682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 373682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 374682f68b0Sdanielk1977 return 0; 375682f68b0Sdanielk1977 } 376f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 377682f68b0Sdanielk1977 } 378682f68b0Sdanielk1977 379682f68b0Sdanielk1977 /* 380682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 381682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 382682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 383682f68b0Sdanielk1977 */ 384682f68b0Sdanielk1977 void sqlite3_set_auxdata( 385682f68b0Sdanielk1977 sqlite3_context *pCtx, 386682f68b0Sdanielk1977 int iArg, 387682f68b0Sdanielk1977 void *pAux, 388682f68b0Sdanielk1977 void (*xDelete)(void*) 389682f68b0Sdanielk1977 ){ 390682f68b0Sdanielk1977 struct AuxData *pAuxData; 391f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 392682f68b0Sdanielk1977 if( iArg<0 ) return; 393682f68b0Sdanielk1977 394f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 395f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 396998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 39753f733c7Sdrh pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); 398998da3a2Sdrh if( !pVdbeFunc ) return; 39953f733c7Sdrh pCtx->pVdbeFunc = pVdbeFunc; 4000e3d7476Sdrh memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 4010e3d7476Sdrh sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); 402998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 403998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 404682f68b0Sdanielk1977 } 405682f68b0Sdanielk1977 406f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 407682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 408682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 409682f68b0Sdanielk1977 } 410682f68b0Sdanielk1977 pAuxData->pAux = pAux; 411682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 412682f68b0Sdanielk1977 } 413682f68b0Sdanielk1977 414682f68b0Sdanielk1977 /* 415eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 416eb2e176aSdrh ** called. 417eb2e176aSdrh ** 418cf85a51cSdrh ** This function is deprecated. Do not use it for new code. It is 419cf85a51cSdrh ** provide only to avoid breaking legacy code. New aggregate function 420cf85a51cSdrh ** implementations should keep their own counts within their aggregate 421cf85a51cSdrh ** context. 422eb2e176aSdrh */ 423eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 424eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 425abfcea25Sdrh return p->pMem->n; 426eb2e176aSdrh } 427eb2e176aSdrh 428eb2e176aSdrh /* 4294f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 4304f26d6c4Sdrh */ 4314f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 4324f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4331bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 4344f26d6c4Sdrh } 4354f26d6c4Sdrh 4364f26d6c4Sdrh /* 4374f26d6c4Sdrh ** Return the number of values available from the current row of the 4384f26d6c4Sdrh ** currently executing statement pStmt. 4394f26d6c4Sdrh */ 4404f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 4414f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4421bcdb0c0Sdrh if( pVm==0 || !pVm->resOnStack ) return 0; 4434f26d6c4Sdrh return pVm->nResColumn; 4444f26d6c4Sdrh } 4454f26d6c4Sdrh 4464f26d6c4Sdrh 4474f26d6c4Sdrh /* 4484f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 4494f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 4504f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 4514f26d6c4Sdrh ** of NULL. 4524f26d6c4Sdrh */ 4534f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 4544f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4554f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 456*b21f87ddSdrh if( pVm==0 || pVm->resOnStack==0 || i>=pVm->nResColumn || i<0 ){ 4577a521cfbSdrh static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL }; 4584f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 4590f7eb611Sdrh return (Mem*)&nullMem; 4604f26d6c4Sdrh } 4614f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 4624f26d6c4Sdrh } 4634f26d6c4Sdrh 4642e588c75Sdanielk1977 /* 4652e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 4662e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 4672e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 4682e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 4692e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 4702e588c75Sdanielk1977 ** 4712e588c75Sdanielk1977 ** Specificly, this is called from within: 4722e588c75Sdanielk1977 ** 4732e588c75Sdanielk1977 ** sqlite3_column_int() 4742e588c75Sdanielk1977 ** sqlite3_column_int64() 4752e588c75Sdanielk1977 ** sqlite3_column_text() 4762e588c75Sdanielk1977 ** sqlite3_column_text16() 4772e588c75Sdanielk1977 ** sqlite3_column_real() 4782e588c75Sdanielk1977 ** sqlite3_column_bytes() 4792e588c75Sdanielk1977 ** sqlite3_column_bytes16() 4802e588c75Sdanielk1977 ** 4812e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc(). 4822e588c75Sdanielk1977 */ 4832e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 4842e588c75Sdanielk1977 { 4852e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 4862e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 4872e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 4882e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 4892e588c75Sdanielk1977 */ 49054f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 49154f0198eSdanielk1977 p->rc = sqlite3ApiExit(0, p->rc); 4922e588c75Sdanielk1977 } 4932e588c75Sdanielk1977 4944f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 4954f26d6c4Sdrh ** The following routines are used to access elements of the current row 4964f26d6c4Sdrh ** in the result set. 4974f26d6c4Sdrh */ 498c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 4992e588c75Sdanielk1977 const void *val; 5002e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 501c9cf901dSdanielk1977 /* Even though there is no encoding conversion, value_blob() might 502c9cf901dSdanielk1977 ** need to call malloc() to expand the result of a zeroblob() 503c9cf901dSdanielk1977 ** expression. 504c9cf901dSdanielk1977 */ 505c9cf901dSdanielk1977 columnMallocFailure(pStmt); 5062e588c75Sdanielk1977 return val; 507c572ef7fSdanielk1977 } 5084f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 5092e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 5102e588c75Sdanielk1977 columnMallocFailure(pStmt); 5112e588c75Sdanielk1977 return val; 5124f26d6c4Sdrh } 5134f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 5142e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 5152e588c75Sdanielk1977 columnMallocFailure(pStmt); 5162e588c75Sdanielk1977 return val; 5174f26d6c4Sdrh } 5184f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 5192e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 5202e588c75Sdanielk1977 columnMallocFailure(pStmt); 5212e588c75Sdanielk1977 return val; 5224f26d6c4Sdrh } 5234f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 5242e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 5252e588c75Sdanielk1977 columnMallocFailure(pStmt); 5262e588c75Sdanielk1977 return val; 5274f26d6c4Sdrh } 528efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 5292e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 5302e588c75Sdanielk1977 columnMallocFailure(pStmt); 5312e588c75Sdanielk1977 return val; 5324f26d6c4Sdrh } 5334f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 5342e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 5352e588c75Sdanielk1977 columnMallocFailure(pStmt); 5362e588c75Sdanielk1977 return val; 5374f26d6c4Sdrh } 538d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 539d1e4733dSdrh return columnMem(pStmt, i); 540d1e4733dSdrh } 5416c62608fSdrh #ifndef SQLITE_OMIT_UTF16 5424f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 5432e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 5442e588c75Sdanielk1977 columnMallocFailure(pStmt); 5452e588c75Sdanielk1977 return val; 5464f26d6c4Sdrh } 5476c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 5484f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 5494f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 5504f26d6c4Sdrh } 5514f26d6c4Sdrh 55229d72108Sdrh /* The following function is experimental and subject to change or 55329d72108Sdrh ** removal */ 55429d72108Sdrh /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 55529d72108Sdrh ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 55629d72108Sdrh **} 55729d72108Sdrh */ 55829d72108Sdrh 5595e2517e1Sdrh /* 5605e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 5615e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 562e590fbdeSdrh ** 563e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 564e590fbdeSdrh ** name is returned. Here are the names: 565e590fbdeSdrh ** 566e590fbdeSdrh ** 0 The column name as it should be displayed for output 567e590fbdeSdrh ** 1 The datatype name for the column 568e590fbdeSdrh ** 2 The name of the database that the column derives from 569e590fbdeSdrh ** 3 The name of the table that the column derives from 570e590fbdeSdrh ** 4 The name of the table column that the result column derives from 571e590fbdeSdrh ** 572e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 573e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 5745e2517e1Sdrh */ 5755e2517e1Sdrh static const void *columnName( 5765e2517e1Sdrh sqlite3_stmt *pStmt, 5775e2517e1Sdrh int N, 5785e2517e1Sdrh const void *(*xFunc)(Mem*), 5795e2517e1Sdrh int useType 5805e2517e1Sdrh ){ 58100fd957bSdanielk1977 const void *ret; 5825e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 5835e2517e1Sdrh int n = sqlite3_column_count(pStmt); 5845e2517e1Sdrh 5855e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 5865e2517e1Sdrh return 0; 5875e2517e1Sdrh } 588e590fbdeSdrh N += useType*n; 58900fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 59000fd957bSdanielk1977 59100fd957bSdanielk1977 /* A malloc may have failed inside of the xFunc() call. If this is the case, 59200fd957bSdanielk1977 ** clear the mallocFailed flag and return NULL. 59300fd957bSdanielk1977 */ 59454f0198eSdanielk1977 sqlite3ApiExit(0, 0); 59500fd957bSdanielk1977 return ret; 5965e2517e1Sdrh } 5975e2517e1Sdrh 5984f26d6c4Sdrh /* 5994f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 6004f26d6c4Sdrh ** statement pStmt. 6014f26d6c4Sdrh */ 6024f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 603955de52cSdanielk1977 return columnName( 604955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 6054f26d6c4Sdrh } 606e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 607e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 608955de52cSdanielk1977 return columnName( 609955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 610e590fbdeSdrh } 611e590fbdeSdrh #endif 6124f26d6c4Sdrh 6134f26d6c4Sdrh /* 6146c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 615e590fbdeSdrh ** of the result set of SQL statement pStmt. 6166c62608fSdrh */ 6176c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 618955de52cSdanielk1977 return columnName( 619955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 6206c62608fSdrh } 6216c62608fSdrh #ifndef SQLITE_OMIT_UTF16 62276d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 623955de52cSdanielk1977 return columnName( 624955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 6254f26d6c4Sdrh } 6266c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 6274f26d6c4Sdrh 6284b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA 629e590fbdeSdrh /* 630e590fbdeSdrh ** Return the name of the database from which a result column derives. 631e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 632e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 633e590fbdeSdrh */ 634e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 635955de52cSdanielk1977 return columnName( 636955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 637e590fbdeSdrh } 638e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 639e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 640955de52cSdanielk1977 return columnName( 641955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 642e590fbdeSdrh } 643e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 644e590fbdeSdrh 645e590fbdeSdrh /* 646e590fbdeSdrh ** Return the name of the table from which a result column derives. 647e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 648e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 649e590fbdeSdrh */ 650e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 651955de52cSdanielk1977 return columnName( 652955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 653e590fbdeSdrh } 654e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 655e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 656955de52cSdanielk1977 return columnName( 657955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 658e590fbdeSdrh } 659e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 660e590fbdeSdrh 661e590fbdeSdrh /* 662e590fbdeSdrh ** Return the name of the table column from which a result column derives. 663e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 664e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 665e590fbdeSdrh */ 666e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 667955de52cSdanielk1977 return columnName( 668955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 669e590fbdeSdrh } 670e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 671e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 672955de52cSdanielk1977 return columnName( 673955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 674e590fbdeSdrh } 675e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 6764b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 677e590fbdeSdrh 678e590fbdeSdrh 6794f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 6804f26d6c4Sdrh ** 6814f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 6824f26d6c4Sdrh */ 6834f26d6c4Sdrh /* 6844f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 6854f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 6864f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 6874f26d6c4Sdrh ** 6884f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 6894f26d6c4Sdrh ** value in any case. 6904f26d6c4Sdrh */ 6914f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 6924f26d6c4Sdrh Mem *pVar; 6931bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 694473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 6954f26d6c4Sdrh return SQLITE_MISUSE; 6964f26d6c4Sdrh } 6974f26d6c4Sdrh if( i<1 || i>p->nVar ){ 6984f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 6994f26d6c4Sdrh return SQLITE_RANGE; 7004f26d6c4Sdrh } 7014f26d6c4Sdrh i--; 702290c1948Sdrh pVar = &p->aVar[i]; 703d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 7044f26d6c4Sdrh pVar->flags = MEM_Null; 7054f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 7064f26d6c4Sdrh return SQLITE_OK; 7074f26d6c4Sdrh } 7084f26d6c4Sdrh 7094f26d6c4Sdrh /* 7105e2517e1Sdrh ** Bind a text or BLOB value. 7114f26d6c4Sdrh */ 7125e2517e1Sdrh static int bindText( 713f4479501Sdrh sqlite3_stmt *pStmt, 7144f26d6c4Sdrh int i, 7154f26d6c4Sdrh const void *zData, 7164f26d6c4Sdrh int nData, 7175e2517e1Sdrh void (*xDel)(void*), 7185e2517e1Sdrh int encoding 7194f26d6c4Sdrh ){ 7204f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7214f26d6c4Sdrh Mem *pVar; 7224f26d6c4Sdrh int rc; 7234f26d6c4Sdrh 7244f26d6c4Sdrh rc = vdbeUnbind(p, i); 725feac5f8dSdrh if( rc || zData==0 ){ 7264f26d6c4Sdrh return rc; 7274f26d6c4Sdrh } 728290c1948Sdrh pVar = &p->aVar[i-1]; 7295e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 7305e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 73114db2665Sdanielk1977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 7325e2517e1Sdrh } 73354f0198eSdanielk1977 734771151b6Sdanielk1977 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); 73554f0198eSdanielk1977 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); 7365e2517e1Sdrh } 7375e2517e1Sdrh 7385e2517e1Sdrh 7395e2517e1Sdrh /* 7405e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 7415e2517e1Sdrh */ 7425e2517e1Sdrh int sqlite3_bind_blob( 7435e2517e1Sdrh sqlite3_stmt *pStmt, 7445e2517e1Sdrh int i, 7455e2517e1Sdrh const void *zData, 7465e2517e1Sdrh int nData, 7475e2517e1Sdrh void (*xDel)(void*) 7485e2517e1Sdrh ){ 7495e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 7505e2517e1Sdrh } 7514f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 7524f26d6c4Sdrh int rc; 7534f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7544f26d6c4Sdrh rc = vdbeUnbind(p, i); 7554f26d6c4Sdrh if( rc==SQLITE_OK ){ 756290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 7574f26d6c4Sdrh } 758f4618891Sdanielk1977 return rc; 7594f26d6c4Sdrh } 7604f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 761efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 7624f26d6c4Sdrh } 763efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 7644f26d6c4Sdrh int rc; 7654f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7664f26d6c4Sdrh rc = vdbeUnbind(p, i); 7674f26d6c4Sdrh if( rc==SQLITE_OK ){ 768290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 7694f26d6c4Sdrh } 7704f26d6c4Sdrh return rc; 7714f26d6c4Sdrh } 7724f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 7734f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 7744f26d6c4Sdrh } 7754f26d6c4Sdrh int sqlite3_bind_text( 7764f26d6c4Sdrh sqlite3_stmt *pStmt, 7774f26d6c4Sdrh int i, 7784f26d6c4Sdrh const char *zData, 7794f26d6c4Sdrh int nData, 780d8123366Sdanielk1977 void (*xDel)(void*) 7814f26d6c4Sdrh ){ 7825e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 7834f26d6c4Sdrh } 7846c62608fSdrh #ifndef SQLITE_OMIT_UTF16 7854f26d6c4Sdrh int sqlite3_bind_text16( 7864f26d6c4Sdrh sqlite3_stmt *pStmt, 7874f26d6c4Sdrh int i, 7884f26d6c4Sdrh const void *zData, 7894f26d6c4Sdrh int nData, 790d8123366Sdanielk1977 void (*xDel)(void*) 7914f26d6c4Sdrh ){ 7925e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 7934f26d6c4Sdrh } 7946c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 79526e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 79626e4144dSdanielk1977 int rc; 79726e4144dSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 79826e4144dSdanielk1977 rc = vdbeUnbind(p, i); 79926e4144dSdanielk1977 if( rc==SQLITE_OK ){ 80026e4144dSdanielk1977 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); 80126e4144dSdanielk1977 } 80226e4144dSdanielk1977 return rc; 80326e4144dSdanielk1977 } 804b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 805b026e05eSdrh int rc; 806b026e05eSdrh Vdbe *p = (Vdbe *)pStmt; 807b026e05eSdrh rc = vdbeUnbind(p, i); 808b026e05eSdrh if( rc==SQLITE_OK ){ 809b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 810b026e05eSdrh } 811b026e05eSdrh return rc; 812b026e05eSdrh } 81375f6a032Sdrh 81475f6a032Sdrh /* 81575f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 81675f6a032Sdrh ** This routine is added to support DBD::SQLite. 81775f6a032Sdrh */ 81875f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 81992febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 82092febd92Sdrh return p ? p->nVar : 0; 82175f6a032Sdrh } 822895d7472Sdrh 823895d7472Sdrh /* 824fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 825fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 826fa6bc000Sdrh ** exist. 827895d7472Sdrh */ 828fa6bc000Sdrh static void createVarMap(Vdbe *p){ 829895d7472Sdrh if( !p->okVar ){ 830895d7472Sdrh int j; 831895d7472Sdrh Op *pOp; 832895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 833895d7472Sdrh if( pOp->opcode==OP_Variable ){ 834895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 835895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 836895d7472Sdrh } 837895d7472Sdrh } 838895d7472Sdrh p->okVar = 1; 839895d7472Sdrh } 840fa6bc000Sdrh } 841fa6bc000Sdrh 842fa6bc000Sdrh /* 843fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 844fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 845fa6bc000Sdrh ** 846fa6bc000Sdrh ** The result is always UTF-8. 847fa6bc000Sdrh */ 848fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 849fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 850fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 851fa6bc000Sdrh return 0; 852fa6bc000Sdrh } 853fa6bc000Sdrh createVarMap(p); 854895d7472Sdrh return p->azVar[i-1]; 855895d7472Sdrh } 856fa6bc000Sdrh 857fa6bc000Sdrh /* 858fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 859fa6bc000Sdrh ** with that name. If there is no variable with the given name, 860fa6bc000Sdrh ** return 0. 861fa6bc000Sdrh */ 862fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 863fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 864fa6bc000Sdrh int i; 865fa6bc000Sdrh if( p==0 ){ 866fa6bc000Sdrh return 0; 867fa6bc000Sdrh } 868fa6bc000Sdrh createVarMap(p); 8696a8903c3Sdrh if( zName ){ 870fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 871971a7c87Sdrh const char *z = p->azVar[i]; 8726a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 873fa6bc000Sdrh return i+1; 874fa6bc000Sdrh } 875fa6bc000Sdrh } 8766a8903c3Sdrh } 877fa6bc000Sdrh return 0; 878fa6bc000Sdrh } 879f8db1bc0Sdrh 88051942bc3Sdrh /* 881f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 882f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 883f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 884f8db1bc0Sdrh */ 885f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 886f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 887f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 888f8db1bc0Sdrh int i, rc = SQLITE_OK; 889f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 890f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 891f8db1bc0Sdrh return SQLITE_MISUSE; 892f8db1bc0Sdrh } 893f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 894f8db1bc0Sdrh return SQLITE_ERROR; 895f8db1bc0Sdrh } 896f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 897950f054cSdanielk1977 sqlite3MallocDisallow(); 898f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 899950f054cSdanielk1977 sqlite3MallocAllow(); 900f8db1bc0Sdrh } 9014ac285a1Sdrh assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 902f8db1bc0Sdrh return rc; 903f8db1bc0Sdrh } 90451942bc3Sdrh 90551942bc3Sdrh /* 90651942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 90751942bc3Sdrh ** in the argument belongs. This is the same database handle that was 90851942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 90951942bc3Sdrh ** the statement in the first place. 91051942bc3Sdrh */ 91151942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 91251942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 91351942bc3Sdrh } 914