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 */ 278*612642deSdanielk1977 sqlite3Error(p->db, p->rc, 0); 279b900aaf3Sdrh return p->rc; 280b900aaf3Sdrh }else{ 281b900aaf3Sdrh /* This is for legacy sqlite3_prepare() builds and when the code 282b900aaf3Sdrh ** is SQLITE_ROW or SQLITE_DONE */ 28376e8d1a2Sdanielk1977 return rc; 2844f26d6c4Sdrh } 285b900aaf3Sdrh } 286b900aaf3Sdrh 287b900aaf3Sdrh /* 288b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step(). Call 289b900aaf3Sdrh ** sqlite3Step() to do most of the work. If a schema error occurs, 290b900aaf3Sdrh ** call sqlite3Reprepare() and try again. 291b900aaf3Sdrh */ 292b900aaf3Sdrh #ifdef SQLITE_OMIT_PARSER 293b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 294b900aaf3Sdrh return sqlite3Step((Vdbe*)pStmt); 295b900aaf3Sdrh } 296b900aaf3Sdrh #else 297b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 298b900aaf3Sdrh int cnt = 0; 299b900aaf3Sdrh int rc; 300b900aaf3Sdrh Vdbe *v = (Vdbe*)pStmt; 301b900aaf3Sdrh while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 302b900aaf3Sdrh && cnt++ < 5 303b900aaf3Sdrh && sqlite3Reprepare(v) ){ 304b900aaf3Sdrh sqlite3_reset(pStmt); 305b900aaf3Sdrh v->expired = 0; 306b900aaf3Sdrh } 307b900aaf3Sdrh return rc; 308b900aaf3Sdrh } 309b900aaf3Sdrh #endif 3104f26d6c4Sdrh 3114f26d6c4Sdrh /* 312eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 313eb2e176aSdrh ** pointer to it. 314eb2e176aSdrh */ 315eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 316eb2e176aSdrh assert( p && p->pFunc ); 317eb2e176aSdrh return p->pFunc->pUserData; 318eb2e176aSdrh } 319eb2e176aSdrh 320eb2e176aSdrh /* 321b7481e70Sdrh ** The following is the implementation of an SQL function that always 322b7481e70Sdrh ** fails with an error message stating that the function is used in the 323b7481e70Sdrh ** wrong context. The sqlite3_overload_function() API might construct 324b7481e70Sdrh ** SQL function that use this routine so that the functions will exist 325b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction 326b7481e70Sdrh ** method of virtual tables. 327b7481e70Sdrh */ 328b7481e70Sdrh void sqlite3InvalidFunction( 329b7481e70Sdrh sqlite3_context *context, /* The function calling context */ 330b7481e70Sdrh int argc, /* Number of arguments to the function */ 331b7481e70Sdrh sqlite3_value **argv /* Value of each argument */ 332b7481e70Sdrh ){ 333b7481e70Sdrh const char *zName = context->pFunc->zName; 334b7481e70Sdrh char *zErr; 335b7481e70Sdrh zErr = sqlite3MPrintf( 336b7481e70Sdrh "unable to use function %s in the requested context", zName); 337b7481e70Sdrh sqlite3_result_error(context, zErr, -1); 338b7481e70Sdrh sqliteFree(zErr); 339b7481e70Sdrh } 340b7481e70Sdrh 341b7481e70Sdrh /* 342eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 343eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 344eb2e176aSdrh ** same context that was returned on prior calls. 345eb2e176aSdrh */ 346eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 347abfcea25Sdrh Mem *pMem = p->pMem; 348825c662eSdrh assert( p && p->pFunc && p->pFunc->xStep ); 349a10a34b8Sdrh if( (pMem->flags & MEM_Agg)==0 ){ 350a10a34b8Sdrh if( nByte==0 ){ 351a10a34b8Sdrh assert( pMem->flags==MEM_Null ); 352a10a34b8Sdrh pMem->z = 0; 353a10a34b8Sdrh }else{ 354abfcea25Sdrh pMem->flags = MEM_Agg; 3555360ad34Sdrh pMem->xDel = sqlite3FreeX; 3563c024d69Sdrh pMem->u.pDef = p->pFunc; 357eb2e176aSdrh if( nByte<=NBFS ){ 358abfcea25Sdrh pMem->z = pMem->zShort; 359abfcea25Sdrh memset(pMem->z, 0, nByte); 360eb2e176aSdrh }else{ 361abfcea25Sdrh pMem->z = sqliteMalloc( nByte ); 362eb2e176aSdrh } 363eb2e176aSdrh } 364a10a34b8Sdrh } 365abfcea25Sdrh return (void*)pMem->z; 366eb2e176aSdrh } 367eb2e176aSdrh 368eb2e176aSdrh /* 369682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 370682f68b0Sdanielk1977 ** the user-function defined by pCtx. 371682f68b0Sdanielk1977 */ 372682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 373682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 374682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 375682f68b0Sdanielk1977 return 0; 376682f68b0Sdanielk1977 } 377f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 378682f68b0Sdanielk1977 } 379682f68b0Sdanielk1977 380682f68b0Sdanielk1977 /* 381682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 382682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 383682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 384682f68b0Sdanielk1977 */ 385682f68b0Sdanielk1977 void sqlite3_set_auxdata( 386682f68b0Sdanielk1977 sqlite3_context *pCtx, 387682f68b0Sdanielk1977 int iArg, 388682f68b0Sdanielk1977 void *pAux, 389682f68b0Sdanielk1977 void (*xDelete)(void*) 390682f68b0Sdanielk1977 ){ 391682f68b0Sdanielk1977 struct AuxData *pAuxData; 392f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 393682f68b0Sdanielk1977 if( iArg<0 ) return; 394682f68b0Sdanielk1977 395f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 396f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 397998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 39853f733c7Sdrh pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); 399998da3a2Sdrh if( !pVdbeFunc ) return; 40053f733c7Sdrh pCtx->pVdbeFunc = pVdbeFunc; 4010e3d7476Sdrh memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 4020e3d7476Sdrh sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); 403998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 404998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 405682f68b0Sdanielk1977 } 406682f68b0Sdanielk1977 407f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 408682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 409682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 410682f68b0Sdanielk1977 } 411682f68b0Sdanielk1977 pAuxData->pAux = pAux; 412682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 413682f68b0Sdanielk1977 } 414682f68b0Sdanielk1977 415682f68b0Sdanielk1977 /* 416eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 417eb2e176aSdrh ** called. 418eb2e176aSdrh ** 419cf85a51cSdrh ** This function is deprecated. Do not use it for new code. It is 420cf85a51cSdrh ** provide only to avoid breaking legacy code. New aggregate function 421cf85a51cSdrh ** implementations should keep their own counts within their aggregate 422cf85a51cSdrh ** context. 423eb2e176aSdrh */ 424eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 425eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 426abfcea25Sdrh return p->pMem->n; 427eb2e176aSdrh } 428eb2e176aSdrh 429eb2e176aSdrh /* 4304f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 4314f26d6c4Sdrh */ 4324f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 4334f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4341bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 4354f26d6c4Sdrh } 4364f26d6c4Sdrh 4374f26d6c4Sdrh /* 4384f26d6c4Sdrh ** Return the number of values available from the current row of the 4394f26d6c4Sdrh ** currently executing statement pStmt. 4404f26d6c4Sdrh */ 4414f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 4424f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4431bcdb0c0Sdrh if( pVm==0 || !pVm->resOnStack ) return 0; 4444f26d6c4Sdrh return pVm->nResColumn; 4454f26d6c4Sdrh } 4464f26d6c4Sdrh 4474f26d6c4Sdrh 4484f26d6c4Sdrh /* 4494f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 4504f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 4514f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 4524f26d6c4Sdrh ** of NULL. 4534f26d6c4Sdrh */ 4544f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 4554f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4564f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 457b21f87ddSdrh if( pVm==0 || pVm->resOnStack==0 || i>=pVm->nResColumn || i<0 ){ 4587a521cfbSdrh static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL }; 4594f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 4600f7eb611Sdrh return (Mem*)&nullMem; 4614f26d6c4Sdrh } 4624f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 4634f26d6c4Sdrh } 4644f26d6c4Sdrh 4652e588c75Sdanielk1977 /* 4662e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 4672e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 4682e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 4692e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 4702e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 4712e588c75Sdanielk1977 ** 4722e588c75Sdanielk1977 ** Specificly, this is called from within: 4732e588c75Sdanielk1977 ** 4742e588c75Sdanielk1977 ** sqlite3_column_int() 4752e588c75Sdanielk1977 ** sqlite3_column_int64() 4762e588c75Sdanielk1977 ** sqlite3_column_text() 4772e588c75Sdanielk1977 ** sqlite3_column_text16() 4782e588c75Sdanielk1977 ** sqlite3_column_real() 4792e588c75Sdanielk1977 ** sqlite3_column_bytes() 4802e588c75Sdanielk1977 ** sqlite3_column_bytes16() 4812e588c75Sdanielk1977 ** 4822e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc(). 4832e588c75Sdanielk1977 */ 4842e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 4852e588c75Sdanielk1977 { 4862e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 4872e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 4882e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 4892e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 4902e588c75Sdanielk1977 */ 49154f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 49254f0198eSdanielk1977 p->rc = sqlite3ApiExit(0, p->rc); 4932e588c75Sdanielk1977 } 4942e588c75Sdanielk1977 4954f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 4964f26d6c4Sdrh ** The following routines are used to access elements of the current row 4974f26d6c4Sdrh ** in the result set. 4984f26d6c4Sdrh */ 499c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 5002e588c75Sdanielk1977 const void *val; 5012e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 502c9cf901dSdanielk1977 /* Even though there is no encoding conversion, value_blob() might 503c9cf901dSdanielk1977 ** need to call malloc() to expand the result of a zeroblob() 504c9cf901dSdanielk1977 ** expression. 505c9cf901dSdanielk1977 */ 506c9cf901dSdanielk1977 columnMallocFailure(pStmt); 5072e588c75Sdanielk1977 return val; 508c572ef7fSdanielk1977 } 5094f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 5102e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 5112e588c75Sdanielk1977 columnMallocFailure(pStmt); 5122e588c75Sdanielk1977 return val; 5134f26d6c4Sdrh } 5144f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 5152e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 5162e588c75Sdanielk1977 columnMallocFailure(pStmt); 5172e588c75Sdanielk1977 return val; 5184f26d6c4Sdrh } 5194f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 5202e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 5212e588c75Sdanielk1977 columnMallocFailure(pStmt); 5222e588c75Sdanielk1977 return val; 5234f26d6c4Sdrh } 5244f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 5252e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 5262e588c75Sdanielk1977 columnMallocFailure(pStmt); 5272e588c75Sdanielk1977 return val; 5284f26d6c4Sdrh } 529efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 5302e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 5312e588c75Sdanielk1977 columnMallocFailure(pStmt); 5322e588c75Sdanielk1977 return val; 5334f26d6c4Sdrh } 5344f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 5352e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 5362e588c75Sdanielk1977 columnMallocFailure(pStmt); 5372e588c75Sdanielk1977 return val; 5384f26d6c4Sdrh } 539d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 540d1e4733dSdrh return columnMem(pStmt, i); 541d1e4733dSdrh } 5426c62608fSdrh #ifndef SQLITE_OMIT_UTF16 5434f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 5442e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 5452e588c75Sdanielk1977 columnMallocFailure(pStmt); 5462e588c75Sdanielk1977 return val; 5474f26d6c4Sdrh } 5486c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 5494f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 5504f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 5514f26d6c4Sdrh } 5524f26d6c4Sdrh 55329d72108Sdrh /* The following function is experimental and subject to change or 55429d72108Sdrh ** removal */ 55529d72108Sdrh /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 55629d72108Sdrh ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 55729d72108Sdrh **} 55829d72108Sdrh */ 55929d72108Sdrh 5605e2517e1Sdrh /* 5615e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 5625e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 563e590fbdeSdrh ** 564e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 565e590fbdeSdrh ** name is returned. Here are the names: 566e590fbdeSdrh ** 567e590fbdeSdrh ** 0 The column name as it should be displayed for output 568e590fbdeSdrh ** 1 The datatype name for the column 569e590fbdeSdrh ** 2 The name of the database that the column derives from 570e590fbdeSdrh ** 3 The name of the table that the column derives from 571e590fbdeSdrh ** 4 The name of the table column that the result column derives from 572e590fbdeSdrh ** 573e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 574e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 5755e2517e1Sdrh */ 5765e2517e1Sdrh static const void *columnName( 5775e2517e1Sdrh sqlite3_stmt *pStmt, 5785e2517e1Sdrh int N, 5795e2517e1Sdrh const void *(*xFunc)(Mem*), 5805e2517e1Sdrh int useType 5815e2517e1Sdrh ){ 58200fd957bSdanielk1977 const void *ret; 5835e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 5845e2517e1Sdrh int n = sqlite3_column_count(pStmt); 5855e2517e1Sdrh 5865e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 5875e2517e1Sdrh return 0; 5885e2517e1Sdrh } 589e590fbdeSdrh N += useType*n; 59000fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 59100fd957bSdanielk1977 59200fd957bSdanielk1977 /* A malloc may have failed inside of the xFunc() call. If this is the case, 59300fd957bSdanielk1977 ** clear the mallocFailed flag and return NULL. 59400fd957bSdanielk1977 */ 59554f0198eSdanielk1977 sqlite3ApiExit(0, 0); 59600fd957bSdanielk1977 return ret; 5975e2517e1Sdrh } 5985e2517e1Sdrh 5994f26d6c4Sdrh /* 6004f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 6014f26d6c4Sdrh ** statement pStmt. 6024f26d6c4Sdrh */ 6034f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 604955de52cSdanielk1977 return columnName( 605955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 6064f26d6c4Sdrh } 607e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 608e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 609955de52cSdanielk1977 return columnName( 610955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 611e590fbdeSdrh } 612e590fbdeSdrh #endif 6134f26d6c4Sdrh 6144f26d6c4Sdrh /* 6156c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 616e590fbdeSdrh ** of the result set of SQL statement pStmt. 6176c62608fSdrh */ 6186c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 619955de52cSdanielk1977 return columnName( 620955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 6216c62608fSdrh } 6226c62608fSdrh #ifndef SQLITE_OMIT_UTF16 62376d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 624955de52cSdanielk1977 return columnName( 625955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 6264f26d6c4Sdrh } 6276c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 6284f26d6c4Sdrh 6294b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA 630e590fbdeSdrh /* 631e590fbdeSdrh ** Return the name of the database from which a result column derives. 632e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 633e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 634e590fbdeSdrh */ 635e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 636955de52cSdanielk1977 return columnName( 637955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 638e590fbdeSdrh } 639e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 640e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 641955de52cSdanielk1977 return columnName( 642955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 643e590fbdeSdrh } 644e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 645e590fbdeSdrh 646e590fbdeSdrh /* 647e590fbdeSdrh ** Return the name of the table from which a result column derives. 648e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 649e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 650e590fbdeSdrh */ 651e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 652955de52cSdanielk1977 return columnName( 653955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 654e590fbdeSdrh } 655e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 656e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 657955de52cSdanielk1977 return columnName( 658955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 659e590fbdeSdrh } 660e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 661e590fbdeSdrh 662e590fbdeSdrh /* 663e590fbdeSdrh ** Return the name of the table column from which a result column derives. 664e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 665e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 666e590fbdeSdrh */ 667e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 668955de52cSdanielk1977 return columnName( 669955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 670e590fbdeSdrh } 671e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 672e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 673955de52cSdanielk1977 return columnName( 674955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 675e590fbdeSdrh } 676e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 6774b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 678e590fbdeSdrh 679e590fbdeSdrh 6804f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 6814f26d6c4Sdrh ** 6824f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 6834f26d6c4Sdrh */ 6844f26d6c4Sdrh /* 6854f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 6864f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 6874f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 6884f26d6c4Sdrh ** 6894f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 6904f26d6c4Sdrh ** value in any case. 6914f26d6c4Sdrh */ 6924f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 6934f26d6c4Sdrh Mem *pVar; 6941bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 695473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 6964f26d6c4Sdrh return SQLITE_MISUSE; 6974f26d6c4Sdrh } 6984f26d6c4Sdrh if( i<1 || i>p->nVar ){ 6994f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 7004f26d6c4Sdrh return SQLITE_RANGE; 7014f26d6c4Sdrh } 7024f26d6c4Sdrh i--; 703290c1948Sdrh pVar = &p->aVar[i]; 704d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 7054f26d6c4Sdrh pVar->flags = MEM_Null; 7064f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 7074f26d6c4Sdrh return SQLITE_OK; 7084f26d6c4Sdrh } 7094f26d6c4Sdrh 7104f26d6c4Sdrh /* 7115e2517e1Sdrh ** Bind a text or BLOB value. 7124f26d6c4Sdrh */ 7135e2517e1Sdrh static int bindText( 714f4479501Sdrh sqlite3_stmt *pStmt, 7154f26d6c4Sdrh int i, 7164f26d6c4Sdrh const void *zData, 7174f26d6c4Sdrh int nData, 7185e2517e1Sdrh void (*xDel)(void*), 7195e2517e1Sdrh int encoding 7204f26d6c4Sdrh ){ 7214f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7224f26d6c4Sdrh Mem *pVar; 7234f26d6c4Sdrh int rc; 7244f26d6c4Sdrh 7254f26d6c4Sdrh rc = vdbeUnbind(p, i); 726feac5f8dSdrh if( rc || zData==0 ){ 7274f26d6c4Sdrh return rc; 7284f26d6c4Sdrh } 729290c1948Sdrh pVar = &p->aVar[i-1]; 7305e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 7315e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 73214db2665Sdanielk1977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 7335e2517e1Sdrh } 73454f0198eSdanielk1977 735771151b6Sdanielk1977 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); 73654f0198eSdanielk1977 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); 7375e2517e1Sdrh } 7385e2517e1Sdrh 7395e2517e1Sdrh 7405e2517e1Sdrh /* 7415e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 7425e2517e1Sdrh */ 7435e2517e1Sdrh int sqlite3_bind_blob( 7445e2517e1Sdrh sqlite3_stmt *pStmt, 7455e2517e1Sdrh int i, 7465e2517e1Sdrh const void *zData, 7475e2517e1Sdrh int nData, 7485e2517e1Sdrh void (*xDel)(void*) 7495e2517e1Sdrh ){ 7505e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 7515e2517e1Sdrh } 7524f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 7534f26d6c4Sdrh int rc; 7544f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7554f26d6c4Sdrh rc = vdbeUnbind(p, i); 7564f26d6c4Sdrh if( rc==SQLITE_OK ){ 757290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 7584f26d6c4Sdrh } 759f4618891Sdanielk1977 return rc; 7604f26d6c4Sdrh } 7614f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 762efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 7634f26d6c4Sdrh } 764efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 7654f26d6c4Sdrh int rc; 7664f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7674f26d6c4Sdrh rc = vdbeUnbind(p, i); 7684f26d6c4Sdrh if( rc==SQLITE_OK ){ 769290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 7704f26d6c4Sdrh } 7714f26d6c4Sdrh return rc; 7724f26d6c4Sdrh } 7734f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 7744f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 7754f26d6c4Sdrh } 7764f26d6c4Sdrh int sqlite3_bind_text( 7774f26d6c4Sdrh sqlite3_stmt *pStmt, 7784f26d6c4Sdrh int i, 7794f26d6c4Sdrh const char *zData, 7804f26d6c4Sdrh int nData, 781d8123366Sdanielk1977 void (*xDel)(void*) 7824f26d6c4Sdrh ){ 7835e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 7844f26d6c4Sdrh } 7856c62608fSdrh #ifndef SQLITE_OMIT_UTF16 7864f26d6c4Sdrh int sqlite3_bind_text16( 7874f26d6c4Sdrh sqlite3_stmt *pStmt, 7884f26d6c4Sdrh int i, 7894f26d6c4Sdrh const void *zData, 7904f26d6c4Sdrh int nData, 791d8123366Sdanielk1977 void (*xDel)(void*) 7924f26d6c4Sdrh ){ 7935e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 7944f26d6c4Sdrh } 7956c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 79626e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 79726e4144dSdanielk1977 int rc; 79826e4144dSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 79926e4144dSdanielk1977 rc = vdbeUnbind(p, i); 80026e4144dSdanielk1977 if( rc==SQLITE_OK ){ 80126e4144dSdanielk1977 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); 80226e4144dSdanielk1977 } 80326e4144dSdanielk1977 return rc; 80426e4144dSdanielk1977 } 805b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 806b026e05eSdrh int rc; 807b026e05eSdrh Vdbe *p = (Vdbe *)pStmt; 808b026e05eSdrh rc = vdbeUnbind(p, i); 809b026e05eSdrh if( rc==SQLITE_OK ){ 810b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 811b026e05eSdrh } 812b026e05eSdrh return rc; 813b026e05eSdrh } 81475f6a032Sdrh 81575f6a032Sdrh /* 81675f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 81775f6a032Sdrh ** This routine is added to support DBD::SQLite. 81875f6a032Sdrh */ 81975f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 82092febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 82192febd92Sdrh return p ? p->nVar : 0; 82275f6a032Sdrh } 823895d7472Sdrh 824895d7472Sdrh /* 825fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 826fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 827fa6bc000Sdrh ** exist. 828895d7472Sdrh */ 829fa6bc000Sdrh static void createVarMap(Vdbe *p){ 830895d7472Sdrh if( !p->okVar ){ 831895d7472Sdrh int j; 832895d7472Sdrh Op *pOp; 833895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 834895d7472Sdrh if( pOp->opcode==OP_Variable ){ 835895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 836895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 837895d7472Sdrh } 838895d7472Sdrh } 839895d7472Sdrh p->okVar = 1; 840895d7472Sdrh } 841fa6bc000Sdrh } 842fa6bc000Sdrh 843fa6bc000Sdrh /* 844fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 845fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 846fa6bc000Sdrh ** 847fa6bc000Sdrh ** The result is always UTF-8. 848fa6bc000Sdrh */ 849fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 850fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 851fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 852fa6bc000Sdrh return 0; 853fa6bc000Sdrh } 854fa6bc000Sdrh createVarMap(p); 855895d7472Sdrh return p->azVar[i-1]; 856895d7472Sdrh } 857fa6bc000Sdrh 858fa6bc000Sdrh /* 859fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 860fa6bc000Sdrh ** with that name. If there is no variable with the given name, 861fa6bc000Sdrh ** return 0. 862fa6bc000Sdrh */ 863fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 864fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 865fa6bc000Sdrh int i; 866fa6bc000Sdrh if( p==0 ){ 867fa6bc000Sdrh return 0; 868fa6bc000Sdrh } 869fa6bc000Sdrh createVarMap(p); 8706a8903c3Sdrh if( zName ){ 871fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 872971a7c87Sdrh const char *z = p->azVar[i]; 8736a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 874fa6bc000Sdrh return i+1; 875fa6bc000Sdrh } 876fa6bc000Sdrh } 8776a8903c3Sdrh } 878fa6bc000Sdrh return 0; 879fa6bc000Sdrh } 880f8db1bc0Sdrh 88151942bc3Sdrh /* 882f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 883f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 884f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 885f8db1bc0Sdrh */ 886f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 887f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 888f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 889f8db1bc0Sdrh int i, rc = SQLITE_OK; 890f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 891f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 892f8db1bc0Sdrh return SQLITE_MISUSE; 893f8db1bc0Sdrh } 894f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 895f8db1bc0Sdrh return SQLITE_ERROR; 896f8db1bc0Sdrh } 897f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 898950f054cSdanielk1977 sqlite3MallocDisallow(); 899f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 900950f054cSdanielk1977 sqlite3MallocAllow(); 901f8db1bc0Sdrh } 9024ac285a1Sdrh assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 903f8db1bc0Sdrh return rc; 904f8db1bc0Sdrh } 90551942bc3Sdrh 90651942bc3Sdrh /* 90751942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 90851942bc3Sdrh ** in the argument belongs. This is the same database handle that was 90951942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 91051942bc3Sdrh ** the statement in the first place. 91151942bc3Sdrh */ 91251942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 91351942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 91451942bc3Sdrh } 915