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 194f26d6c4Sdrh /**************************** sqlite3_value_ ******************************* 204f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value 214f26d6c4Sdrh ** structure. 224f26d6c4Sdrh */ 234f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){ 244f26d6c4Sdrh Mem *p = (Mem*)pVal; 254f26d6c4Sdrh if( p->flags & (MEM_Blob|MEM_Str) ){ 264f26d6c4Sdrh return p->z; 274f26d6c4Sdrh }else{ 284f26d6c4Sdrh return sqlite3_value_text(pVal); 294f26d6c4Sdrh } 304f26d6c4Sdrh } 314f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 324f26d6c4Sdrh Mem *p = (Mem*)pVal; 334f26d6c4Sdrh if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text(pVal) ){ 344f26d6c4Sdrh return p->n; 354f26d6c4Sdrh } 364f26d6c4Sdrh return 0; 374f26d6c4Sdrh } 384f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 394f26d6c4Sdrh Mem *p = (Mem*)pVal; 404f26d6c4Sdrh if( (p->flags & MEM_Blob)!=0 || sqlite3_value_text16(pVal) ){ 414f26d6c4Sdrh return ((Mem *)pVal)->n; 424f26d6c4Sdrh } 434f26d6c4Sdrh return 0; 444f26d6c4Sdrh } 454f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 464f26d6c4Sdrh Mem *pMem = (Mem *)pVal; 47f4479501Sdrh sqlite3VdbeMemRealify(pMem); 484f26d6c4Sdrh return pMem->r; 494f26d6c4Sdrh } 504f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 514f26d6c4Sdrh Mem *pMem = (Mem *)pVal; 52f4479501Sdrh sqlite3VdbeMemIntegerify(pMem); 534f26d6c4Sdrh return (int)pVal->i; 544f26d6c4Sdrh } 554f26d6c4Sdrh long long int sqlite3_value_int64(sqlite3_value *pVal){ 564f26d6c4Sdrh Mem *pMem = (Mem *)pVal; 57f4479501Sdrh sqlite3VdbeMemIntegerify(pMem); 584f26d6c4Sdrh return pVal->i; 594f26d6c4Sdrh } 604f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 61*dc8453fdSdanielk1977 return (const char *)sqlite3ValueText(pVal, SQLITE_UTF8); 624f26d6c4Sdrh } 634f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 64*dc8453fdSdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 654f26d6c4Sdrh } 664f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 67f4479501Sdrh return pVal->type; 684f26d6c4Sdrh } 694f26d6c4Sdrh 704f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 714f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 724f26d6c4Sdrh ** the function result. 734f26d6c4Sdrh */ 744f26d6c4Sdrh void sqlite3_result_blob( 754f26d6c4Sdrh sqlite3_context *pCtx, 764f26d6c4Sdrh const void *z, 774f26d6c4Sdrh int n, 784f26d6c4Sdrh int eCopy 794f26d6c4Sdrh ){ 804f26d6c4Sdrh assert( n>0 ); 81f4479501Sdrh sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, eCopy); 824f26d6c4Sdrh } 834f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 844f26d6c4Sdrh sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 854f26d6c4Sdrh } 864f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 874f26d6c4Sdrh pCtx->isError = 1; 88*dc8453fdSdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, 1); 894f26d6c4Sdrh } 904f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 914f26d6c4Sdrh pCtx->isError = 1; 92*dc8453fdSdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, 1); 934f26d6c4Sdrh } 94f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 954f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 964f26d6c4Sdrh } 974f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 984f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 994f26d6c4Sdrh } 1004f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 101f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 1024f26d6c4Sdrh } 1034f26d6c4Sdrh void sqlite3_result_text( 1044f26d6c4Sdrh sqlite3_context *pCtx, 1054f26d6c4Sdrh const char *z, 1064f26d6c4Sdrh int n, 1074f26d6c4Sdrh int eCopy 1084f26d6c4Sdrh ){ 109*dc8453fdSdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, eCopy); 1104f26d6c4Sdrh } 1114f26d6c4Sdrh void sqlite3_result_text16( 1124f26d6c4Sdrh sqlite3_context *pCtx, 1134f26d6c4Sdrh const void *z, 1144f26d6c4Sdrh int n, 1154f26d6c4Sdrh int eCopy 1164f26d6c4Sdrh ){ 117*dc8453fdSdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, eCopy); 1184f26d6c4Sdrh } 1194f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 1204f26d6c4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 1214f26d6c4Sdrh } 1224f26d6c4Sdrh 1234f26d6c4Sdrh 1244f26d6c4Sdrh /* 1254f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 1264f26d6c4Sdrh ** statement is completely executed or an error occurs. 1274f26d6c4Sdrh */ 1284f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 1294f26d6c4Sdrh Vdbe *p = (Vdbe*)pStmt; 1304f26d6c4Sdrh sqlite *db; 1314f26d6c4Sdrh int rc; 1324f26d6c4Sdrh 1334f26d6c4Sdrh if( p->magic!=VDBE_MAGIC_RUN ){ 1344f26d6c4Sdrh return SQLITE_MISUSE; 1354f26d6c4Sdrh } 1364f26d6c4Sdrh db = p->db; 1374f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1384f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1394f26d6c4Sdrh return SQLITE_MISUSE; 1404f26d6c4Sdrh } 1411d850a72Sdanielk1977 if( p->pc<0 ){ 1421d850a72Sdanielk1977 db->activeVdbeCnt++; 1431d850a72Sdanielk1977 p->pc = 0; 1441d850a72Sdanielk1977 } 1454f26d6c4Sdrh if( p->explain ){ 1464f26d6c4Sdrh rc = sqlite3VdbeList(p); 1474f26d6c4Sdrh }else{ 1484f26d6c4Sdrh rc = sqlite3VdbeExec(p); 1494f26d6c4Sdrh } 1504f26d6c4Sdrh 1514f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 1524f26d6c4Sdrh rc = SQLITE_MISUSE; 1534f26d6c4Sdrh } 1544f26d6c4Sdrh 1554f26d6c4Sdrh sqlite3Error(p->db, rc, p->zErrMsg); 1564f26d6c4Sdrh return rc; 1574f26d6c4Sdrh } 1584f26d6c4Sdrh 1594f26d6c4Sdrh /* 160eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 161eb2e176aSdrh ** pointer to it. 162eb2e176aSdrh */ 163eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 164eb2e176aSdrh assert( p && p->pFunc ); 165eb2e176aSdrh return p->pFunc->pUserData; 166eb2e176aSdrh } 167eb2e176aSdrh 168eb2e176aSdrh /* 169eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 170eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 171eb2e176aSdrh ** same context that was returned on prior calls. 172eb2e176aSdrh ** 173eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing 174eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in 175eb2e176aSdrh ** this source file. 176eb2e176aSdrh */ 177eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 178eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 179eb2e176aSdrh if( p->pAgg==0 ){ 180eb2e176aSdrh if( nByte<=NBFS ){ 181eb2e176aSdrh p->pAgg = (void*)p->s.z; 182eb2e176aSdrh memset(p->pAgg, 0, nByte); 183eb2e176aSdrh }else{ 184eb2e176aSdrh p->pAgg = sqliteMalloc( nByte ); 185eb2e176aSdrh } 186eb2e176aSdrh } 187eb2e176aSdrh return p->pAgg; 188eb2e176aSdrh } 189eb2e176aSdrh 190eb2e176aSdrh /* 191682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 192682f68b0Sdanielk1977 ** the user-function defined by pCtx. 193682f68b0Sdanielk1977 */ 194682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 195682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 196682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 197682f68b0Sdanielk1977 return 0; 198682f68b0Sdanielk1977 } 199682f68b0Sdanielk1977 return pCtx->pVdbeFunc->apAux[iArg].pAux; 200682f68b0Sdanielk1977 } 201682f68b0Sdanielk1977 202682f68b0Sdanielk1977 /* 203682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 204682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 205682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 206682f68b0Sdanielk1977 */ 207682f68b0Sdanielk1977 void sqlite3_set_auxdata( 208682f68b0Sdanielk1977 sqlite3_context *pCtx, 209682f68b0Sdanielk1977 int iArg, 210682f68b0Sdanielk1977 void *pAux, 211682f68b0Sdanielk1977 void (*xDelete)(void*) 212682f68b0Sdanielk1977 ){ 213682f68b0Sdanielk1977 struct AuxData *pAuxData; 214682f68b0Sdanielk1977 if( iArg<0 ) return; 215682f68b0Sdanielk1977 216682f68b0Sdanielk1977 if( !pCtx->pVdbeFunc || pCtx->pVdbeFunc->nAux<=iArg ){ 217682f68b0Sdanielk1977 int nMalloc = sizeof(VdbeFunc)+sizeof(struct AuxData)*(iArg+1); 218682f68b0Sdanielk1977 pCtx->pVdbeFunc = sqliteRealloc(pCtx->pVdbeFunc, nMalloc); 219682f68b0Sdanielk1977 if( !pCtx->pVdbeFunc ) return; 220682f68b0Sdanielk1977 pCtx->pVdbeFunc->nAux = iArg+1; 221d02eb1fdSdanielk1977 pCtx->pVdbeFunc->pFunc = pCtx->pFunc; 222682f68b0Sdanielk1977 } 223682f68b0Sdanielk1977 224682f68b0Sdanielk1977 pAuxData = &pCtx->pVdbeFunc->apAux[iArg]; 225682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 226682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 227682f68b0Sdanielk1977 } 228682f68b0Sdanielk1977 pAuxData->pAux = pAux; 229682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 230682f68b0Sdanielk1977 } 231682f68b0Sdanielk1977 232682f68b0Sdanielk1977 /* 233eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 234eb2e176aSdrh ** called. 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 int sqlite3_aggregate_count(sqlite3_context *p){ 241eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 242eb2e176aSdrh return p->cnt; 243eb2e176aSdrh } 244eb2e176aSdrh 245eb2e176aSdrh /* 2464f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 2474f26d6c4Sdrh */ 2484f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 2494f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 2504f26d6c4Sdrh return pVm->nResColumn; 2514f26d6c4Sdrh } 2524f26d6c4Sdrh 2534f26d6c4Sdrh /* 2544f26d6c4Sdrh ** Return the number of values available from the current row of the 2554f26d6c4Sdrh ** currently executing statement pStmt. 2564f26d6c4Sdrh */ 2574f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 2584f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 2594f26d6c4Sdrh if( !pVm->resOnStack ) return 0; 2604f26d6c4Sdrh return pVm->nResColumn; 2614f26d6c4Sdrh } 2624f26d6c4Sdrh 2634f26d6c4Sdrh 2644f26d6c4Sdrh /* 2654f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 2664f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 2674f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 2684f26d6c4Sdrh ** of NULL. 2694f26d6c4Sdrh */ 2704f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 2714f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 2724f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 2734f26d6c4Sdrh if( i>=vals || i<0 ){ 2744f26d6c4Sdrh static Mem nullMem; 2754f26d6c4Sdrh if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } 2764f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 2774f26d6c4Sdrh return &nullMem; 2784f26d6c4Sdrh } 2794f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 2804f26d6c4Sdrh } 2814f26d6c4Sdrh 2824f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 2834f26d6c4Sdrh ** The following routines are used to access elements of the current row 2844f26d6c4Sdrh ** in the result set. 2854f26d6c4Sdrh */ 286c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 287c572ef7fSdanielk1977 return sqlite3_value_blob( columnMem(pStmt,i) ); 288c572ef7fSdanielk1977 } 2894f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 2904f26d6c4Sdrh return sqlite3_value_bytes( columnMem(pStmt,i) ); 2914f26d6c4Sdrh } 2924f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 2934f26d6c4Sdrh return sqlite3_value_bytes16( columnMem(pStmt,i) ); 2944f26d6c4Sdrh } 2954f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 2964f26d6c4Sdrh return sqlite3_value_double( columnMem(pStmt,i) ); 2974f26d6c4Sdrh } 2984f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 2994f26d6c4Sdrh return sqlite3_value_int( columnMem(pStmt,i) ); 3004f26d6c4Sdrh } 3014f26d6c4Sdrh long long int sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 3024f26d6c4Sdrh return sqlite3_value_int64( columnMem(pStmt,i) ); 3034f26d6c4Sdrh } 3044f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 3054f26d6c4Sdrh return sqlite3_value_text( columnMem(pStmt,i) ); 3064f26d6c4Sdrh } 3074f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 3084f26d6c4Sdrh return sqlite3_value_text16( columnMem(pStmt,i) ); 3094f26d6c4Sdrh } 3104f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 3114f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 3124f26d6c4Sdrh } 3134f26d6c4Sdrh 3144f26d6c4Sdrh 3154f26d6c4Sdrh /* 3164f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 3174f26d6c4Sdrh ** statement pStmt. 3184f26d6c4Sdrh */ 3194f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 3204f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 3214f26d6c4Sdrh Mem *pColName; 3224f26d6c4Sdrh 3234f26d6c4Sdrh if( N>=sqlite3_column_count(pStmt) || N<0 ){ 3244f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 3254f26d6c4Sdrh return 0; 3264f26d6c4Sdrh } 3274f26d6c4Sdrh 3284f26d6c4Sdrh pColName = &(p->aColName[N]); 3294f26d6c4Sdrh return sqlite3_value_text(pColName); 3304f26d6c4Sdrh } 3314f26d6c4Sdrh 3324f26d6c4Sdrh /* 3334f26d6c4Sdrh ** Return the name of the 'i'th column of the result set of SQL statement 3344f26d6c4Sdrh ** pStmt, encoded as UTF-16. 3354f26d6c4Sdrh */ 3364f26d6c4Sdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 3374f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 3384f26d6c4Sdrh Mem *pColName; 3394f26d6c4Sdrh 3404f26d6c4Sdrh if( N>=sqlite3_column_count(pStmt) || N<0 ){ 3414f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 3424f26d6c4Sdrh return 0; 3434f26d6c4Sdrh } 3444f26d6c4Sdrh 3454f26d6c4Sdrh pColName = &(p->aColName[N]); 3464f26d6c4Sdrh return sqlite3_value_text16(pColName); 3474f26d6c4Sdrh } 3484f26d6c4Sdrh 3494f26d6c4Sdrh /* 3504f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column 3514f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-8. 3524f26d6c4Sdrh */ 35376d505baSdanielk1977 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 3544f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 35576d505baSdanielk1977 Mem *pColName; 3564f26d6c4Sdrh 35776d505baSdanielk1977 if( N>=sqlite3_column_count(pStmt) || N<0 ){ 3584f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 3594f26d6c4Sdrh return 0; 3604f26d6c4Sdrh } 3614f26d6c4Sdrh 36276d505baSdanielk1977 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]); 36376d505baSdanielk1977 return sqlite3_value_text(pColName); 3644f26d6c4Sdrh } 3654f26d6c4Sdrh 3664f26d6c4Sdrh /* 3674f26d6c4Sdrh ** Return the column declaration type (if applicable) of the 'i'th column 3684f26d6c4Sdrh ** of the result set of SQL statement pStmt, encoded as UTF-16. 3694f26d6c4Sdrh */ 37076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 37176d505baSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 37276d505baSdanielk1977 Mem *pColName; 37376d505baSdanielk1977 37476d505baSdanielk1977 if( N>=sqlite3_column_count(pStmt) || N<0 ){ 37576d505baSdanielk1977 sqlite3Error(p->db, SQLITE_RANGE, 0); 37676d505baSdanielk1977 return 0; 37776d505baSdanielk1977 } 37876d505baSdanielk1977 37976d505baSdanielk1977 pColName = &(p->aColName[N+sqlite3_column_count(pStmt)]); 38076d505baSdanielk1977 return sqlite3_value_text16(pColName); 3814f26d6c4Sdrh } 3824f26d6c4Sdrh 3834f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 3844f26d6c4Sdrh ** 3854f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 3864f26d6c4Sdrh */ 3874f26d6c4Sdrh /* 3884f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 3894f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 3904f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 3914f26d6c4Sdrh ** 3924f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 3934f26d6c4Sdrh ** value in any case. 3944f26d6c4Sdrh */ 3954f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 3964f26d6c4Sdrh Mem *pVar; 3971d850a72Sdanielk1977 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 3984f26d6c4Sdrh sqlite3Error(p->db, SQLITE_MISUSE, 0); 3994f26d6c4Sdrh return SQLITE_MISUSE; 4004f26d6c4Sdrh } 4014f26d6c4Sdrh if( i<1 || i>p->nVar ){ 4024f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 4034f26d6c4Sdrh return SQLITE_RANGE; 4044f26d6c4Sdrh } 4054f26d6c4Sdrh i--; 4064f26d6c4Sdrh pVar = &p->apVar[i]; 4074f26d6c4Sdrh if( pVar->flags&MEM_Dyn ){ 4084f26d6c4Sdrh sqliteFree(pVar->z); 4094f26d6c4Sdrh } 4104f26d6c4Sdrh pVar->flags = MEM_Null; 4114f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 4124f26d6c4Sdrh return SQLITE_OK; 4134f26d6c4Sdrh } 4144f26d6c4Sdrh 4154f26d6c4Sdrh /* 4164f26d6c4Sdrh ** Bind a blob value to an SQL statement variable. 4174f26d6c4Sdrh */ 4184f26d6c4Sdrh int sqlite3_bind_blob( 419f4479501Sdrh sqlite3_stmt *pStmt, 4204f26d6c4Sdrh int i, 4214f26d6c4Sdrh const void *zData, 4224f26d6c4Sdrh int nData, 4234f26d6c4Sdrh int eCopy 4244f26d6c4Sdrh ){ 4254f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 4264f26d6c4Sdrh Mem *pVar; 4274f26d6c4Sdrh int rc; 4284f26d6c4Sdrh 4294f26d6c4Sdrh rc = vdbeUnbind(p, i); 4304f26d6c4Sdrh if( rc ){ 4314f26d6c4Sdrh return rc; 4324f26d6c4Sdrh } 4334f26d6c4Sdrh pVar = &p->apVar[i-1]; 4344f26d6c4Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, 0, eCopy); 4354f26d6c4Sdrh return rc; 4364f26d6c4Sdrh } 4374f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 4384f26d6c4Sdrh int rc; 4394f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 4404f26d6c4Sdrh rc = vdbeUnbind(p, i); 4414f26d6c4Sdrh if( rc==SQLITE_OK ){ 442f4479501Sdrh sqlite3VdbeMemSetDouble(&p->apVar[i-1], rValue); 4434f26d6c4Sdrh } 4444f26d6c4Sdrh return SQLITE_OK; 4454f26d6c4Sdrh } 4464f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 4474f26d6c4Sdrh return sqlite3_bind_int64(p, i, (long long int)iValue); 4484f26d6c4Sdrh } 4494f26d6c4Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, long long int iValue){ 4504f26d6c4Sdrh int rc; 4514f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 4524f26d6c4Sdrh rc = vdbeUnbind(p, i); 4534f26d6c4Sdrh if( rc==SQLITE_OK ){ 454f4479501Sdrh sqlite3VdbeMemSetInt64(&p->apVar[i-1], iValue); 4554f26d6c4Sdrh } 4564f26d6c4Sdrh return rc; 4574f26d6c4Sdrh } 4584f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 4594f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 4604f26d6c4Sdrh } 4614f26d6c4Sdrh int sqlite3_bind_text( 4624f26d6c4Sdrh sqlite3_stmt *pStmt, 4634f26d6c4Sdrh int i, 4644f26d6c4Sdrh const char *zData, 4654f26d6c4Sdrh int nData, 4664f26d6c4Sdrh int eCopy 4674f26d6c4Sdrh ){ 4684f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 4694f26d6c4Sdrh Mem *pVar; 4704f26d6c4Sdrh int rc; 4714f26d6c4Sdrh 4724f26d6c4Sdrh rc = vdbeUnbind(p, i); 4734f26d6c4Sdrh if( rc ){ 4744f26d6c4Sdrh return rc; 4754f26d6c4Sdrh } 4764f26d6c4Sdrh pVar = &p->apVar[i-1]; 477*dc8453fdSdanielk1977 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, SQLITE_UTF8, eCopy); 4784f26d6c4Sdrh if( rc ){ 4794f26d6c4Sdrh return rc; 4804f26d6c4Sdrh } 481f4479501Sdrh rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); 4824f26d6c4Sdrh return rc; 4834f26d6c4Sdrh } 4844f26d6c4Sdrh int sqlite3_bind_text16( 4854f26d6c4Sdrh sqlite3_stmt *pStmt, 4864f26d6c4Sdrh int i, 4874f26d6c4Sdrh const void *zData, 4884f26d6c4Sdrh int nData, 4894f26d6c4Sdrh int eCopy 4904f26d6c4Sdrh ){ 4914f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 4924f26d6c4Sdrh Mem *pVar; 493f4479501Sdrh int rc, txt_enc; 4944f26d6c4Sdrh 4954f26d6c4Sdrh rc = vdbeUnbind(p, i); 4964f26d6c4Sdrh if( rc ){ 4974f26d6c4Sdrh return rc; 4984f26d6c4Sdrh } 499f4479501Sdrh pVar = &p->apVar[i-1]; 5004f26d6c4Sdrh 5014f26d6c4Sdrh /* There may or may not be a byte order mark at the start of the UTF-16. 502*dc8453fdSdanielk1977 ** Either way set 'txt_enc' to the SQLITE_UTF16* value indicating the 5034f26d6c4Sdrh ** actual byte order used by this string. If the string does happen 5044f26d6c4Sdrh ** to contain a BOM, then move zData so that it points to the first 5054f26d6c4Sdrh ** byte after the BOM. 5064f26d6c4Sdrh */ 5074f26d6c4Sdrh txt_enc = sqlite3UtfReadBom(zData, nData); 5084f26d6c4Sdrh if( txt_enc ){ 5094f26d6c4Sdrh zData = (void *)(((u8 *)zData) + 2); 5104f26d6c4Sdrh nData -= 2; 5114f26d6c4Sdrh }else{ 512*dc8453fdSdanielk1977 txt_enc = SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE; 5134f26d6c4Sdrh } 5144f26d6c4Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, txt_enc, eCopy); 5154f26d6c4Sdrh if( rc ){ 5164f26d6c4Sdrh return rc; 5174f26d6c4Sdrh } 518f4479501Sdrh rc = sqlite3VdbeChangeEncoding(pVar, p->db->enc); 5194f26d6c4Sdrh return rc; 5204f26d6c4Sdrh } 521