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" 184f26d6c4Sdrh 19d89bd007Sdrh /* 20d89bd007Sdrh ** Return TRUE (non-zero) of the statement supplied as an argument needs 21d89bd007Sdrh ** to be recompiled. A statement needs to be recompiled whenever the 22d89bd007Sdrh ** execution environment changes in a way that would alter the program 23d89bd007Sdrh ** that sqlite3_prepare() generates. For example, if new functions or 24d89bd007Sdrh ** collating sequences are registered or if an authorizer function is 25d89bd007Sdrh ** added or changed. 26d89bd007Sdrh ** 27d89bd007Sdrh ***** EXPERIMENTAL ****** 28d89bd007Sdrh */ 29d89bd007Sdrh int sqlite3_expired(sqlite3_stmt *pStmt){ 30d89bd007Sdrh Vdbe *p = (Vdbe*)pStmt; 31d89bd007Sdrh return p==0 || p->expired; 32d89bd007Sdrh } 33d89bd007Sdrh 344f26d6c4Sdrh /**************************** sqlite3_value_ ******************************* 354f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value 364f26d6c4Sdrh ** structure. 374f26d6c4Sdrh */ 384f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){ 394f26d6c4Sdrh Mem *p = (Mem*)pVal; 404f26d6c4Sdrh if( p->flags & (MEM_Blob|MEM_Str) ){ 414f26d6c4Sdrh return p->z; 424f26d6c4Sdrh }else{ 434f26d6c4Sdrh return sqlite3_value_text(pVal); 444f26d6c4Sdrh } 454f26d6c4Sdrh } 464f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 4713073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 484f26d6c4Sdrh } 494f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 5013073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 514f26d6c4Sdrh } 524f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 536a6124e2Sdrh return sqlite3VdbeRealValue((Mem*)pVal); 544f26d6c4Sdrh } 554f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 566a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 574f26d6c4Sdrh } 58efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 596a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 604f26d6c4Sdrh } 614f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 62dc8453fdSdanielk1977 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8); 634f26d6c4Sdrh } 646c62608fSdrh #ifndef SQLITE_OMIT_UTF16 654f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 66dc8453fdSdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 674f26d6c4Sdrh } 68d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 69d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 70d8123366Sdanielk1977 } 71d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 72d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 73d8123366Sdanielk1977 } 746c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 754f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 76f4479501Sdrh return pVal->type; 774f26d6c4Sdrh } 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 ){ 894f26d6c4Sdrh 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 } 994f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 1004f26d6c4Sdrh pCtx->isError = 1; 101d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 1024f26d6c4Sdrh } 103f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 1044f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 1054f26d6c4Sdrh } 1064f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 1074f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 1084f26d6c4Sdrh } 1094f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 110f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 1114f26d6c4Sdrh } 1124f26d6c4Sdrh void sqlite3_result_text( 1134f26d6c4Sdrh sqlite3_context *pCtx, 1144f26d6c4Sdrh const char *z, 1154f26d6c4Sdrh int n, 116d8123366Sdanielk1977 void (*xDel)(void *) 1174f26d6c4Sdrh ){ 118d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); 1194f26d6c4Sdrh } 1206c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1214f26d6c4Sdrh void sqlite3_result_text16( 1224f26d6c4Sdrh sqlite3_context *pCtx, 1234f26d6c4Sdrh const void *z, 1244f26d6c4Sdrh int n, 125d8123366Sdanielk1977 void (*xDel)(void *) 1264f26d6c4Sdrh ){ 127d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); 128d8123366Sdanielk1977 } 129d8123366Sdanielk1977 void sqlite3_result_text16be( 130d8123366Sdanielk1977 sqlite3_context *pCtx, 131d8123366Sdanielk1977 const void *z, 132d8123366Sdanielk1977 int n, 133d8123366Sdanielk1977 void (*xDel)(void *) 134d8123366Sdanielk1977 ){ 135d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); 136d8123366Sdanielk1977 } 137d8123366Sdanielk1977 void sqlite3_result_text16le( 138d8123366Sdanielk1977 sqlite3_context *pCtx, 139d8123366Sdanielk1977 const void *z, 140d8123366Sdanielk1977 int n, 141d8123366Sdanielk1977 void (*xDel)(void *) 142d8123366Sdanielk1977 ){ 143d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); 1444f26d6c4Sdrh } 1456c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1464f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 1474f26d6c4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 1484f26d6c4Sdrh } 1494f26d6c4Sdrh 1504f26d6c4Sdrh 1514f26d6c4Sdrh /* 1524f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 1534f26d6c4Sdrh ** statement is completely executed or an error occurs. 1544f26d6c4Sdrh */ 1554f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 1564f26d6c4Sdrh Vdbe *p = (Vdbe*)pStmt; 1579bb575fdSdrh sqlite3 *db; 1584f26d6c4Sdrh int rc; 1594f26d6c4Sdrh 1601bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ 1614f26d6c4Sdrh return SQLITE_MISUSE; 1624f26d6c4Sdrh } 16391b48aa1Sdrh if( p->aborted ){ 16491b48aa1Sdrh return SQLITE_ABORT; 16591b48aa1Sdrh } 166a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 167a21c6b6fSdanielk1977 if( p->rc==SQLITE_OK ){ 168a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 169a21c6b6fSdanielk1977 } 170a21c6b6fSdanielk1977 return SQLITE_ERROR; 171a21c6b6fSdanielk1977 } 1724f26d6c4Sdrh db = p->db; 1734f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1744f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1754f26d6c4Sdrh return SQLITE_MISUSE; 1764f26d6c4Sdrh } 1771d850a72Sdanielk1977 if( p->pc<0 ){ 178c16a03b5Sdrh /* Invoke the trace callback if there is one 179c16a03b5Sdrh */ 180c16a03b5Sdrh if( (db = p->db)->xTrace && !db->init.busy ){ 181c16a03b5Sdrh assert( p->nOp>0 ); 182c16a03b5Sdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 183c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3!=0 ); 184c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 185c16a03b5Sdrh sqlite3SafetyOff(db); 186c16a03b5Sdrh db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); 187c16a03b5Sdrh if( sqlite3SafetyOn(db) ){ 188c16a03b5Sdrh p->rc = SQLITE_MISUSE; 189c16a03b5Sdrh return SQLITE_MISUSE; 190c16a03b5Sdrh } 191c16a03b5Sdrh } 192c16a03b5Sdrh 193c16a03b5Sdrh /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned 194c16a03b5Sdrh ** on in debugging mode. 195c16a03b5Sdrh */ 196c16a03b5Sdrh #ifdef SQLITE_DEBUG 197c16a03b5Sdrh if( (db->flags & SQLITE_SqlTrace)!=0 ){ 198c16a03b5Sdrh sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); 199c16a03b5Sdrh } 200c16a03b5Sdrh #endif /* SQLITE_DEBUG */ 201c16a03b5Sdrh 2021d850a72Sdanielk1977 db->activeVdbeCnt++; 2031d850a72Sdanielk1977 p->pc = 0; 2041d850a72Sdanielk1977 } 205b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 2064f26d6c4Sdrh if( p->explain ){ 2074f26d6c4Sdrh rc = sqlite3VdbeList(p); 208b7f9164eSdrh }else 209b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 210b7f9164eSdrh { 2114f26d6c4Sdrh rc = sqlite3VdbeExec(p); 2124f26d6c4Sdrh } 2134f26d6c4Sdrh 2144f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 2154f26d6c4Sdrh rc = SQLITE_MISUSE; 2164f26d6c4Sdrh } 2174f26d6c4Sdrh 2184f26d6c4Sdrh sqlite3Error(p->db, rc, p->zErrMsg); 2194f26d6c4Sdrh return rc; 2204f26d6c4Sdrh } 2214f26d6c4Sdrh 2224f26d6c4Sdrh /* 223eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 224eb2e176aSdrh ** pointer to it. 225eb2e176aSdrh */ 226eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 227eb2e176aSdrh assert( p && p->pFunc ); 228eb2e176aSdrh return p->pFunc->pUserData; 229eb2e176aSdrh } 230eb2e176aSdrh 231eb2e176aSdrh /* 232eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 233eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 234eb2e176aSdrh ** same context that was returned on prior calls. 235eb2e176aSdrh ** 236eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing 237eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in 238eb2e176aSdrh ** this source file. 239eb2e176aSdrh */ 240eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 241eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 242eb2e176aSdrh if( p->pAgg==0 ){ 243eb2e176aSdrh if( nByte<=NBFS ){ 244eb2e176aSdrh p->pAgg = (void*)p->s.z; 245eb2e176aSdrh memset(p->pAgg, 0, nByte); 246eb2e176aSdrh }else{ 247eb2e176aSdrh p->pAgg = sqliteMalloc( nByte ); 248eb2e176aSdrh } 249eb2e176aSdrh } 250eb2e176aSdrh return p->pAgg; 251eb2e176aSdrh } 252eb2e176aSdrh 253eb2e176aSdrh /* 254682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 255682f68b0Sdanielk1977 ** the user-function defined by pCtx. 256682f68b0Sdanielk1977 */ 257682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 258682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 259682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 260682f68b0Sdanielk1977 return 0; 261682f68b0Sdanielk1977 } 262f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 263682f68b0Sdanielk1977 } 264682f68b0Sdanielk1977 265682f68b0Sdanielk1977 /* 266682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 267682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 268682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 269682f68b0Sdanielk1977 */ 270682f68b0Sdanielk1977 void sqlite3_set_auxdata( 271682f68b0Sdanielk1977 sqlite3_context *pCtx, 272682f68b0Sdanielk1977 int iArg, 273682f68b0Sdanielk1977 void *pAux, 274682f68b0Sdanielk1977 void (*xDelete)(void*) 275682f68b0Sdanielk1977 ){ 276682f68b0Sdanielk1977 struct AuxData *pAuxData; 277f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 278682f68b0Sdanielk1977 if( iArg<0 ) return; 279682f68b0Sdanielk1977 280f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 281f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 282998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 283f92c7ff7Sdrh pCtx->pVdbeFunc = pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); 284998da3a2Sdrh if( !pVdbeFunc ) return; 2850e3d7476Sdrh memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 2860e3d7476Sdrh sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); 287998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 288998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 289682f68b0Sdanielk1977 } 290682f68b0Sdanielk1977 291f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 292682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 293682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 294682f68b0Sdanielk1977 } 295682f68b0Sdanielk1977 pAuxData->pAux = pAux; 296682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 297682f68b0Sdanielk1977 } 298682f68b0Sdanielk1977 299682f68b0Sdanielk1977 /* 300eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 301eb2e176aSdrh ** called. 302eb2e176aSdrh ** 303eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing 304eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in 305eb2e176aSdrh ** this source file. 306eb2e176aSdrh */ 307eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 308eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 309eb2e176aSdrh return p->cnt; 310eb2e176aSdrh } 311eb2e176aSdrh 312eb2e176aSdrh /* 3134f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 3144f26d6c4Sdrh */ 3154f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 3164f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3171bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 3184f26d6c4Sdrh } 3194f26d6c4Sdrh 3204f26d6c4Sdrh /* 3214f26d6c4Sdrh ** Return the number of values available from the current row of the 3224f26d6c4Sdrh ** currently executing statement pStmt. 3234f26d6c4Sdrh */ 3244f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 3254f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3261bcdb0c0Sdrh if( pVm==0 || !pVm->resOnStack ) return 0; 3274f26d6c4Sdrh return pVm->nResColumn; 3284f26d6c4Sdrh } 3294f26d6c4Sdrh 3304f26d6c4Sdrh 3314f26d6c4Sdrh /* 3324f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 3334f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 3344f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 3354f26d6c4Sdrh ** of NULL. 3364f26d6c4Sdrh */ 3374f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 3384f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3394f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 3404f26d6c4Sdrh if( i>=vals || i<0 ){ 3414f26d6c4Sdrh static Mem nullMem; 3424f26d6c4Sdrh if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } 3434f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 3444f26d6c4Sdrh return &nullMem; 3454f26d6c4Sdrh } 3464f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 3474f26d6c4Sdrh } 3484f26d6c4Sdrh 3494f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 3504f26d6c4Sdrh ** The following routines are used to access elements of the current row 3514f26d6c4Sdrh ** in the result set. 3524f26d6c4Sdrh */ 353c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 354c572ef7fSdanielk1977 return sqlite3_value_blob( columnMem(pStmt,i) ); 355c572ef7fSdanielk1977 } 3564f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 3574f26d6c4Sdrh return sqlite3_value_bytes( columnMem(pStmt,i) ); 3584f26d6c4Sdrh } 3594f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 3604f26d6c4Sdrh return sqlite3_value_bytes16( columnMem(pStmt,i) ); 3614f26d6c4Sdrh } 3624f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 3634f26d6c4Sdrh return sqlite3_value_double( columnMem(pStmt,i) ); 3644f26d6c4Sdrh } 3654f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 3664f26d6c4Sdrh return sqlite3_value_int( columnMem(pStmt,i) ); 3674f26d6c4Sdrh } 368efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 3694f26d6c4Sdrh return sqlite3_value_int64( columnMem(pStmt,i) ); 3704f26d6c4Sdrh } 3714f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 3724f26d6c4Sdrh return sqlite3_value_text( columnMem(pStmt,i) ); 3734f26d6c4Sdrh } 3746c62608fSdrh #ifndef SQLITE_OMIT_UTF16 3754f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 3764f26d6c4Sdrh return sqlite3_value_text16( columnMem(pStmt,i) ); 3774f26d6c4Sdrh } 3786c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 3794f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 3804f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 3814f26d6c4Sdrh } 3824f26d6c4Sdrh 3835e2517e1Sdrh /* 3845e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 3855e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 386e590fbdeSdrh ** 387e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 388e590fbdeSdrh ** name is returned. Here are the names: 389e590fbdeSdrh ** 390e590fbdeSdrh ** 0 The column name as it should be displayed for output 391e590fbdeSdrh ** 1 The datatype name for the column 392e590fbdeSdrh ** 2 The name of the database that the column derives from 393e590fbdeSdrh ** 3 The name of the table that the column derives from 394e590fbdeSdrh ** 4 The name of the table column that the result column derives from 395e590fbdeSdrh ** 396e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 397e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 3985e2517e1Sdrh */ 3995e2517e1Sdrh static const void *columnName( 4005e2517e1Sdrh sqlite3_stmt *pStmt, 4015e2517e1Sdrh int N, 4025e2517e1Sdrh const void *(*xFunc)(Mem*), 4035e2517e1Sdrh int useType 4045e2517e1Sdrh ){ 4055e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 4065e2517e1Sdrh int n = sqlite3_column_count(pStmt); 4075e2517e1Sdrh 4085e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 4095e2517e1Sdrh return 0; 4105e2517e1Sdrh } 411e590fbdeSdrh N += useType*n; 4125e2517e1Sdrh return xFunc(&p->aColName[N]); 4135e2517e1Sdrh } 4145e2517e1Sdrh 4154f26d6c4Sdrh 4164f26d6c4Sdrh /* 4174f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 4184f26d6c4Sdrh ** statement pStmt. 4194f26d6c4Sdrh */ 4204f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 4215e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0); 4224f26d6c4Sdrh } 423e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 424e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 425e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0); 426e590fbdeSdrh } 427e590fbdeSdrh #endif 4284f26d6c4Sdrh 4294f26d6c4Sdrh /* 4306c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 431e590fbdeSdrh ** of the result set of SQL statement pStmt. 4326c62608fSdrh */ 4336c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 4346c62608fSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1); 4356c62608fSdrh } 4366c62608fSdrh #ifndef SQLITE_OMIT_UTF16 43776d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 4385e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1); 4394f26d6c4Sdrh } 4406c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 4414f26d6c4Sdrh 442e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0 443e590fbdeSdrh /* 444e590fbdeSdrh ** Return the name of the database from which a result column derives. 445e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 446e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 447e590fbdeSdrh */ 448e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 449e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2); 450e590fbdeSdrh } 451e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 452e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 453e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2); 454e590fbdeSdrh } 455e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 456e590fbdeSdrh 457e590fbdeSdrh /* 458e590fbdeSdrh ** Return the name of the table from which a result column derives. 459e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 460e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 461e590fbdeSdrh */ 462e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 463e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3); 464e590fbdeSdrh } 465e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 466e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 467e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3); 468e590fbdeSdrh } 469e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 470e590fbdeSdrh 471e590fbdeSdrh /* 472e590fbdeSdrh ** Return the name of the table column from which a result column derives. 473e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 474e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 475e590fbdeSdrh */ 476e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 477e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4); 478e590fbdeSdrh } 479e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 480e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 481e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4); 482e590fbdeSdrh } 483e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 484e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */ 485e590fbdeSdrh 486e590fbdeSdrh 487e590fbdeSdrh 488e590fbdeSdrh 4894f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 4904f26d6c4Sdrh ** 4914f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 4924f26d6c4Sdrh */ 4934f26d6c4Sdrh /* 4944f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 4954f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 4964f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 4974f26d6c4Sdrh ** 4984f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 4994f26d6c4Sdrh ** value in any case. 5004f26d6c4Sdrh */ 5014f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 5024f26d6c4Sdrh Mem *pVar; 5031bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 504*473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 5054f26d6c4Sdrh return SQLITE_MISUSE; 5064f26d6c4Sdrh } 5074f26d6c4Sdrh if( i<1 || i>p->nVar ){ 5084f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 5094f26d6c4Sdrh return SQLITE_RANGE; 5104f26d6c4Sdrh } 5114f26d6c4Sdrh i--; 512290c1948Sdrh pVar = &p->aVar[i]; 513d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 5144f26d6c4Sdrh pVar->flags = MEM_Null; 5154f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 5164f26d6c4Sdrh return SQLITE_OK; 5174f26d6c4Sdrh } 5184f26d6c4Sdrh 5194f26d6c4Sdrh /* 5205e2517e1Sdrh ** Bind a text or BLOB value. 5214f26d6c4Sdrh */ 5225e2517e1Sdrh static int bindText( 523f4479501Sdrh sqlite3_stmt *pStmt, 5244f26d6c4Sdrh int i, 5254f26d6c4Sdrh const void *zData, 5264f26d6c4Sdrh int nData, 5275e2517e1Sdrh void (*xDel)(void*), 5285e2517e1Sdrh int encoding 5294f26d6c4Sdrh ){ 5304f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 5314f26d6c4Sdrh Mem *pVar; 5324f26d6c4Sdrh int rc; 5334f26d6c4Sdrh 5344f26d6c4Sdrh rc = vdbeUnbind(p, i); 535feac5f8dSdrh if( rc || zData==0 ){ 5364f26d6c4Sdrh return rc; 5374f26d6c4Sdrh } 538290c1948Sdrh pVar = &p->aVar[i-1]; 5395e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 5405e2517e1Sdrh if( rc ){ 5414f26d6c4Sdrh return rc; 5424f26d6c4Sdrh } 5435e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 5445e2517e1Sdrh rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); 5455e2517e1Sdrh } 5465e2517e1Sdrh return rc; 5475e2517e1Sdrh } 5485e2517e1Sdrh 5495e2517e1Sdrh 5505e2517e1Sdrh /* 5515e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 5525e2517e1Sdrh */ 5535e2517e1Sdrh int sqlite3_bind_blob( 5545e2517e1Sdrh sqlite3_stmt *pStmt, 5555e2517e1Sdrh int i, 5565e2517e1Sdrh const void *zData, 5575e2517e1Sdrh int nData, 5585e2517e1Sdrh void (*xDel)(void*) 5595e2517e1Sdrh ){ 5605e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 5615e2517e1Sdrh } 5624f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 5634f26d6c4Sdrh int rc; 5644f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 5654f26d6c4Sdrh rc = vdbeUnbind(p, i); 5664f26d6c4Sdrh if( rc==SQLITE_OK ){ 567290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 5684f26d6c4Sdrh } 569f4618891Sdanielk1977 return rc; 5704f26d6c4Sdrh } 5714f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 572efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 5734f26d6c4Sdrh } 574efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 5754f26d6c4Sdrh int rc; 5764f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 5774f26d6c4Sdrh rc = vdbeUnbind(p, i); 5784f26d6c4Sdrh if( rc==SQLITE_OK ){ 579290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 5804f26d6c4Sdrh } 5814f26d6c4Sdrh return rc; 5824f26d6c4Sdrh } 5834f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 5844f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 5854f26d6c4Sdrh } 5864f26d6c4Sdrh int sqlite3_bind_text( 5874f26d6c4Sdrh sqlite3_stmt *pStmt, 5884f26d6c4Sdrh int i, 5894f26d6c4Sdrh const char *zData, 5904f26d6c4Sdrh int nData, 591d8123366Sdanielk1977 void (*xDel)(void*) 5924f26d6c4Sdrh ){ 5935e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 5944f26d6c4Sdrh } 5956c62608fSdrh #ifndef SQLITE_OMIT_UTF16 5964f26d6c4Sdrh int sqlite3_bind_text16( 5974f26d6c4Sdrh sqlite3_stmt *pStmt, 5984f26d6c4Sdrh int i, 5994f26d6c4Sdrh const void *zData, 6004f26d6c4Sdrh int nData, 601d8123366Sdanielk1977 void (*xDel)(void*) 6024f26d6c4Sdrh ){ 6035e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 6044f26d6c4Sdrh } 6056c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 60675f6a032Sdrh 60775f6a032Sdrh /* 60875f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 60975f6a032Sdrh ** This routine is added to support DBD::SQLite. 61075f6a032Sdrh */ 61175f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 61292febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 61392febd92Sdrh return p ? p->nVar : 0; 61475f6a032Sdrh } 615895d7472Sdrh 616895d7472Sdrh /* 617fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 618fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 619fa6bc000Sdrh ** exist. 620895d7472Sdrh */ 621fa6bc000Sdrh static void createVarMap(Vdbe *p){ 622895d7472Sdrh if( !p->okVar ){ 623895d7472Sdrh int j; 624895d7472Sdrh Op *pOp; 625895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 626895d7472Sdrh if( pOp->opcode==OP_Variable ){ 627895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 628895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 629895d7472Sdrh } 630895d7472Sdrh } 631895d7472Sdrh p->okVar = 1; 632895d7472Sdrh } 633fa6bc000Sdrh } 634fa6bc000Sdrh 635fa6bc000Sdrh /* 636fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 637fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 638fa6bc000Sdrh ** 639fa6bc000Sdrh ** The result is always UTF-8. 640fa6bc000Sdrh */ 641fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 642fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 643fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 644fa6bc000Sdrh return 0; 645fa6bc000Sdrh } 646fa6bc000Sdrh createVarMap(p); 647895d7472Sdrh return p->azVar[i-1]; 648895d7472Sdrh } 649fa6bc000Sdrh 650fa6bc000Sdrh /* 651fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 652fa6bc000Sdrh ** with that name. If there is no variable with the given name, 653fa6bc000Sdrh ** return 0. 654fa6bc000Sdrh */ 655fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 656fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 657fa6bc000Sdrh int i; 658fa6bc000Sdrh if( p==0 ){ 659fa6bc000Sdrh return 0; 660fa6bc000Sdrh } 661fa6bc000Sdrh createVarMap(p); 6626a8903c3Sdrh if( zName ){ 663fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 664971a7c87Sdrh const char *z = p->azVar[i]; 6656a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 666fa6bc000Sdrh return i+1; 667fa6bc000Sdrh } 668fa6bc000Sdrh } 6696a8903c3Sdrh } 670fa6bc000Sdrh return 0; 671fa6bc000Sdrh } 672f8db1bc0Sdrh 673f8db1bc0Sdrh /* EXPERIMENTAL 674f8db1bc0Sdrh ** 675f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 676f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 677f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 678f8db1bc0Sdrh */ 679f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 680f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 681f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 682f8db1bc0Sdrh int i, rc = SQLITE_OK; 683f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 684f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 685f8db1bc0Sdrh return SQLITE_MISUSE; 686f8db1bc0Sdrh } 687f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 688f8db1bc0Sdrh return SQLITE_ERROR; 689f8db1bc0Sdrh } 690f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 691f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 692f8db1bc0Sdrh } 693f8db1bc0Sdrh return rc; 694f8db1bc0Sdrh } 695